- 使用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(" "); } //第二种 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(" "); } } }
- 所谓素数(又叫质数)是指只能被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+" "); 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(" "); 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 }