- 使用for循環方式計算2+4+6+…+100的值
1 package day02; 2 /** 3 * 使用for循環方式計算2+4+6+…+100的值 4 * @author mu 5 * 6 */ 7 public class Demo3 { 8 9 public static void main(String[] args) { 10 int sum=0; 11 12 for(int i=2;i<=100;i=i+2){ 13 sum=sum+i; 14 } 15 System.out.println(sum); 16 17 } 18 19 }
- 編寫Java程序,在控制台上打印出九九乘法表(兩種)
package day02; /** * 編寫Java程序,在控制台上打印出九九乘法表(兩種) * @author ASUS * */ public class Demo4 { public static void main(String[] args) { //第一種 for(int m=1;m<=9;m++){ for(int n=1;n<=9;n++){ System.out.print(m+" "+"X"+" "+n+" "+"="+" "+m*n+" "); } System.out.print("\n"); } //第二種 System.out.println("--------------------------------------------------"); for(int m1=1;m1<=9;m1++){ for(int n1=1;n1<=m1;n1++){ System.out.print(m1+" "+"X"+" "+n1+" "+"="+" "+m1*n1+" "); } System.out.print("\n"); } } }
- 所謂素數(又叫質數)是指只能被1和它本身整除的數字,1除外。輸入一個正整數,判斷是否為素數。
1 package day02; 2 3 import java.util.Scanner; 4 5 /** 6 * 練習3: 7 所謂素數(又叫質數)是指只能被1和它本身整除的數字,1除外。輸入一個正整數,判斷是否為素數。 8 * @author ASUS 9 * 10 */ 11 public class Demo5 { 12 13 public static void main(String[] args) { 14 boolean isPrime = true; 15 Scanner sc = new Scanner(System.in); 16 System.out.println("請輸入一個正整數"); 17 int num = sc.nextInt(); 18 if (num > 0) { 19 int k = (int) Math.sqrt(num);//k為num的正平方根,取整數 20 for (int i = 2; i <= k; i++) { 21 if (num % i == 0) { 22 isPrime = false;//不是素數 23 break; 24 } 25 } 26 } 27 if (isPrime) { 28 System.out.println(num + "是素數"); 29 } else { 30 System.out.println(num + "不是素數"); 31 } 32 33 } 34 35 36 }
- 100以內的素數,5個換行
1 package day02; 2 /** 3 * 100以內的素數,5個換行 4 * @author ASUS 5 * 6 */ 7 public class Demo6 { 8 9 public static void main(String[] args) { 10 11 12 boolean isPrime = true; 13 int PrimeCount=0; 14 for (int i = 3; i <= 100; i+=2) { 15 int k = (int) Math.sqrt(i);//k為num的正平方根,取整數 16 isPrime = true; 17 for (int j = 2; j <= k; j++) { 18 if (i % j == 0) { 19 isPrime = false;//不是素數 20 break; 21 } 22 } 23 if (isPrime) { 24 PrimeCount++; 25 System.out.print(i+"\t"); 26 if(PrimeCount%5==0){ 27 System.out.println(); 28 } 29 } 30 } 31 32 33 } 34 35 }
- 循環錄入5個人的年齡,並計算平均年齡,如果錄入的數據出現負數或者大於130的數,立即停止輸出報錯(無需打印平均年齡)
1 package day02; 2 3 import java.util.Scanner; 4 5 /** 6 * 練習5: 7 循環錄入5個人的年齡,並計算平均年齡,如果錄入的數據出現負數或者大於130的數,立即停止輸出報錯(無需打印平均年齡) 8 * @author ASUS 9 * 10 */ 11 public class Demo7 { 12 13 public static void main(String[] args) { 14 int sum =0; 15 Scanner sc = new Scanner(System.in); 16 17 int count=0; 18 do{ 19 for(int i=1;i<=5;i++){ 20 System.out.println("請輸入第"+i+"個人的年齡"); 21 int age = sc.nextInt(); 22 23 24 if(age<0||age>130){ 25 System.out.println("輸入有誤,請輸入0-130的年齡"); 26 break; 27 } 28 29 ++count; 30 sum += age; 31 //aver_age=age/count; 32 33 } 34 }while(count<=4); 35 System.out.println("平均年齡是:"+sum/count); 36 37 38 } 39 40 }
- 使用for循環實現:根據用戶輸入的正整數n,求n!,即n!=n*(n-1)*(n-2)*…*1。
package day02; import java.util.Scanner; /** * 練習6: 使用for循環實現:根據用戶輸入的正整數n,求n!,即n!=n*(n-1)*(n-2)*…*1。 * @author ASUS * i的值是n到n+1 */ public class Demo8 { public static void main(String[] args) { int sum=1; Scanner sc=new Scanner(System.in); System.out.println("請輸入正整數n,求n!"); int n=sc.nextInt(); for (int i = 1; i <= n; i++) { sum=sum*i; } System.out.println("n!="+sum); } }
- 使用循環打印以下聖誕樹:
要求輸入樹的高度,打印聖誕樹。
解題思路:
1、雙層循環
2、高度:h
3、當前行:n
3、空格數:當前行h-n
4、星星數:2n-1
1 package day02; 2 3 import java.util.Scanner; 4 5 /** 6 * 練習7: 7 使用循環打印以下聖誕樹: 8 9 要求輸入樹的高度,打印聖誕樹。 10 11 解題思路: 12 1、雙層循環 13 2、高度:h 14 3、當前行:n 15 3、空格數:當前行h-n 16 4、星星數:2n-1 17 * @author ASUS 18 * 19 */ 20 public class Demo9 { 21 22 public static void main(String[] args) { 23 24 Scanner sc=new Scanner(System.in); 25 System.out.println("請輸入樹的高度:"); 26 int h=sc.nextInt(); 27 for (int i = 1; i <= h; i++) { 28 for (int j = 0; j < h-i; j++) { 29 System.out.print(" "); 30 } 31 for (int k = 0; k < 2*i-1; k++) { 32 System.out.print("*"); 33 } 34 System.out.print("\n"); 35 } 36 37 } 38 39 }
- (百元錢買百只雞問題)一只公雞5元錢,一只母雞3元錢,三只小雞1元錢。要求100元買100只雞,請給出所有可行的結果?
1 package day02; 2 3 /**(百元錢買百只雞問題)一只公雞5元錢,一只母雞3元錢,三只小雞1元錢。要求100元買100只雞,請給出所有可行的結果?*/ 4 public class Demo10 { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 10 int x,y,z; 11 for (x = 0; x <= 20; x++) { 12 for (y = 0; y <= 33; y++) { 13 for (z = 0; z <= 300; z+=3) { 14 if(x+y+z==100&&5*x+3*y+z/3==100){ 15 System.out.println("公雞:"+x+"只,母雞:"+y+"只,小雞:"+z+"只"); 16 } 17 } 18 19 } 20 21 } 22 } 23 24 }