###### tags: `JAVA` `JavaScript`
# JavaScript作業JAVA版
### amstrong
power次方數
例 : 5的4次方 five to the power of four
```
//Amstrong數是指一個三位數的整數,其各位數之立方和等於該數本身。
//找出所有的Amstrong數。
//說明:153=1^3+5^3+3^3,故153為Amstrong數。
public class amstrong {
public static void main(String[] args) {
int hun,ten,di;//百、十、個
for(int i=1;i<1000;i++)//這裡寫要取的amstrong數範圍
{
hun=i/100;
ten=(i%100)/10;//十位
di=i%10;//個位
if(isAmstrong(i,hun,ten,di))
{
System.out.printf("%d\t",i);
}
}
}
private static boolean isAmstrong(int i,int hun,int ten,int di)
{
int sum;
//因為Math.pow預設跑出來的資料型態是double
sum=(int) (Math.pow(hun,3)+Math.pow(ten,3)+Math.pow(di,3));
if(i==sum)
return true;
else
{
return false;
}
}
}
```
---
### expression
```
//利用for迴圈計算1^2-2^2+3^2-4^2+…+49^2-50^2的值。
public class expression {
public static void main(String[] args) {
int max = 50;
int sum = 0;
for (int i = 0; i < max; i++) {
if (i % 2 == 0) {
sum += (i + 1) * (i + 1);
continue;
}
sum -= (i + 1) * (i + 1);
}
System.out.println(sum);
}
}
```
---
### expression(錯的題目)
```
//利用for迴?計算12-22+32-42+…+492-502的值。
public class expression {
public static void main(String[] args) {
int max = 50;
int sum = 0;
for (int i = 0; i < max; i++) {
if (i%2==0) {
sum=sum+Integer.parseInt((i+1)+"2");
continue;
}
sum=sum-Integer.parseInt((i+1)+"2");
}
System.out.println(sum);
}
}
```
---
### factor
```
//輸入一正整數,求其所有的因數。
//說明:36的因數為1, 2, 3, 4, 6, 9, 12, 18, 36。
public class factor {
public static void main(String[] args) {
int i= 36;
showInt(i);
}
private static void showInt(int i) {
System.out.print("數字"+i+"的因數: ");
System.out.print("1");
for (int j = 2; j <=i; j++) {
if(i%j==0)System.out.print(","+j);//餘數0為因數
}
}
}
```
---
### leapYear
```
//輸入一西元年,如2015。判斷此年份是否為閏年。
//提示:每四年一閏,每百年不閏,每四百年一閏。
public class leapYear {
public static void main(String[] args) {
int year = 3;
if ( year%4!=0||(year%100==0 && year%400!=0) ) {
System.out.println(false);
return;
}
System.out.println(true);
// if (year%400==0) {
// System.out.println(true);
// return;
// }
// if (year%100==0) {
// System.out.println(false);
// return;
// }
// if (year%4==0) {
// System.out.println(true);
// return;
// }
// System.out.println(false);
}
}
```
---
### perfectNumber
```
//一個數字若等於其所有因數的總和,則此數為perfect number。
//找出100以內所有的完美數。
//說明:6的因數為1, 2, 3,6=1+2+3,故6為完美數。
public class perfectNumber {
public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
showInt(i);
}
}
private static void showInt(int i) {
int sum = 0;
String str = "其因數為:";
for (int j = 1; j < i; j++) {
if (i % j == 0) {
sum += j;// 餘數0為因數
str += j + ",";
}
}
if (sum == i) {
System.out.println(i + str);
}
}
}
```
---
### perfectNumber小改版
```
//一個數字若等於其所有因數的總和,則此數為perfect number。
//找出100以內所有的完美數。
//說明:6的因數為1, 2, 3,6=1+2+3,故6為完美數。
public class perfectNumber2 {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
showInt(i);
}
}
private static void showInt(int i) {
int sum = 1;
String str = "其因數為:1";
for (int j = 2; j < i; j++) {
if (i % j == 0) {
sum += j;// 餘數0為因數
str += ","+ j;
}
}
if (sum == i) {
System.out.println(i + str);
}
}
}
```
---
### prime
```
//輸入一正整數,找出所有小於或等於的質數。
public class prime {
private static boolean isPrime(int number)
{
for(int j=2;j<number;j++)
{
if(number%j==0) return false; //如果可以被整除,number不是質數
}
return true; //如果整個迴圈跑完都沒有被整除,number是質數]
}
public static void main(String[] args) {
int count=0;//數有幾個質數
for(int i=1;i<=200;i++)
{
if(isPrime(i))
{
//如果isPrime的結果為true,印出質數
count++;
System.out.printf("%d\t",i);
}
}
System.out.printf("\n質數共有%d個",count);
}
}
```
---
### rabbit
```
//老王養了一群兔子,若三隻三隻一數,剩餘一隻;若五隻五隻一數,剩餘三隻;若七隻七隻一數,剩餘二隻。試問兔子最少有幾隻。
public class rabbit {
public static void main(String[] args) {
int maxRabbits = 1000;
for (int i = 0; i < maxRabbits; i++) {
if(i%3==1&&i%5==3&&i%7==2) {
System.out.println(i);
return; //return拿掉可得到其他符合答案的非最少兔子數。
}
}
}
}
```
---
### refund
```
import java.util.ArrayList;
import java.util.List;
//輸入在某商店購物應付金額與實付金額。
//實付金額小於應付金額,則印出”金額不足”。
//實付金額等於應付金額,則印出”不必找錢”。
//實付金額大於應付金額,則輸出找回金額最少的鈔票數和錢幣數。
//假設幣值只有1000, 500, 100元紙鈔和50, 10, 5, 1元硬幣。
//說明:若買了132元的商品,付1000元,應找回一張500元,三張100元,一個50元硬幣,一個10元硬幣,一個5元硬幣和三個1元硬幣。
public class refund {
public String name; //幣種屬性
public int value ;
public int amount;
public refund(String name,int value,int amount) {
this.name = name;
this.value = value;
this.amount = amount;
}
public static void main(String[] args) {
int price = 201;
int pay = 1300;
int change = pay - price; //找錢總金額
List<refund> wallet= new ArrayList<>();
wallet.add(new refund("1000元", 1000, 0));//將幣種從大到小加入
wallet.add(new refund(" 500元", 500, 0));
wallet.add(new refund(" 100元", 100, 0));
wallet.add(new refund(" 50元", 50, 0));
wallet.add(new refund(" 10元", 10, 0));
wallet.add(new refund(" 5元", 5, 0));
wallet.add(new refund(" 1元", 1, 0));
for (refund nowCoin : wallet) { //foreach錢包內的幣種
nowCoin.amount=change/nowCoin.value; //會使用幾個當前幣種
change=change-nowCoin.amount*nowCoin.value; //找錢扣掉已經找完的大鈔
if (nowCoin.amount>0) {
System.out.println("找零 "+nowCoin.name+" "+nowCoin.amount + "份");
}
}
}
}
```
---
### rope
```
//若有一條繩子長3000公尺,每天剪去一半的長度,需多少天繩子的長度會短於5公尺。
public class rope {
public static void main(String[] args) {
double ropeLength = 3000;
double ropeShort = 5;
int count = 0;
while (ropeLength>=ropeShort) {
ropeLength=ropeLength/2;
count+=1;
}
System.out.println(count);
}
}
```
---
### salary
```
//輸入便利商店工讀生的工作時數,並計算其薪資。
//60小時以內,時薪120元。
//61~80小時,以時薪1.25倍計算。150
//81小時以上,以時薪1.5倍計算。180
//說明:薪資以累計方式計算。若工時為90小時,則薪資為60*120 + 20*120*1.25 + 10*120*1.5元。
public class salary {
public static void main(String[] args) {
int hours = 74;
double salary;
salary = hours*120;
if (hours-60>0) {
salary+=(hours-60)*30;
}
if (hours-80>0) {
salary+=(hours-80)*30;
}
System.out.println(salary);
// System.out.println(hours);
// System.out.println(80-hours%80);
// System.out.println(60-hours%80%60);
}
}
```
---
### season
```
//選擇性敘述的練習-season
//輸入月份1~12月,利用switch判斷相對應的季節春、夏、秋、冬並印出。若不在此範圍則印出”輸入錯誤”。
public class season {
public static void main(String[] args) {
int month = 8;
switch (month) {
case 3: case 4: case 5:System.out.println("春");
break;
case 6: case 7: case 8:System.out.println("夏");
break;
case 9: case 10: case 11:System.out.println("秋");
break;
case 1: case 2: case 12:System.out.println("冬");
break;
default:System.out.println("輸入錯誤");
break;
}
}
}
```
---