java第四周做業

1. 分別使用for循環,while循環,do循環求1到100之間全部能被3整除的整數的和。(知識點:循環語句)java

package doom3;

public class thefourweekworks1_0 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i = 1;
		int sum = 0;
		for (i = 1; i <= 100; i++) {
			if (i % 3 == 0)
				sum += i;

		}
		System.out.print(sum);

	}

}

  

package doom3;

public class thefourweekworks1_1 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i=1;
		int sum=0;
		while(i<=100){
			if(i%3==0)
			sum+=i;
			i++;
		}
		System.out.print(sum);
	}

}

  

package doom3;

public class thefourweekworks1_2 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i = 1;
		int sum = 0;
		do {
			if (i % 3 == 0)
				sum += i;
			i++;
		} while (i <= 100);
		System.out.print(sum);
	}

}

 

2. 輸出0-9之間的數,可是不包括5。(知識點:條件、循環語句)3d

package doom3;

public class thefourweekworks2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i=0;
		for(i=0;i<10;i++){
			if(i==5)
			continue;
			System.out.print(" "+i+" ");
		}

	}

}

  

3. 編寫一個程序,求整數n的階乘,例如5的階乘是1*2*3*4*5(知識點:循環語句)code

package doom3;

import java.util.Scanner;

public class thefourweekworks3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);

		System.out.print("請輸入一個數字:");

		int n = input.nextInt();
		int i = 1;
		int sum = 1;
		for (i = 1; i <= n; i++) {
			sum *= i;
		}
		System.out.println(sum);

	}

}

  

4. 編寫一個程序,輸入任意學生成績,若是輸入不合法(<0或者>100),提示輸入錯誤,從新輸入,直到輸入合法程序結束(知識點:循環語句)blog

package doom3;

import java.util.Scanner;

public class thefourweekworks4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.print("請輸入一名學生的成績:");
		int i = input.nextInt();
		while (i < 0 || i > 100) {
			System.out.print("你輸入的成績有問題,請從新輸入:");
			i = input.nextInt();
		}
		System.out.print("你輸入的同窗成績爲:" + i);
	}
}

  

5. 假設某員工今年的年薪是30000元,年薪的年增加率6%。編寫一個Java應用程序計算該員工10年後的年薪,並統計將來10年(從今年算起)總收入。(知識點:循環語句)input

package doom3;

public class thefourweekworks5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i = 0;
		double n = 30000;
		double count = n;
		for (i = 0; i <= 9; i++) {
			n = n * 1.06;
			count = count + n;
		}
		System.out.println("該員工10年後的年薪:" + n);
		System.out.println("該員工10年內的總收入:" + count);

	}

}

  

相關文章
相關標籤/搜索