Java选择结构例题

mac2024-11-17  8

逢7必过

public class Test{ public static void main(String[] args){ int b = 1; //换行作用的变量 for(int i = 1; i <= 100; i++) { //连续输入100个数 if(i % 7 == 0) { //如果是7的倍数则跳过不输出 continue; } if(i % 10 == 7 || i / 10 == 7) { //如果数字含有7则跳过 continue; } if(b < 9) { //不输出这条语句的话输出比较丑 System.out.print(" "); //看起来美观而已 } System.out.print(i + " "); if(b % 7 == 0) { //每7个数字一行 System.out.println(); } b++; } } }

输出结果为:

1 2 3 4 5 6 8 9 10 11 12 13 15 16 18 19 20 22 23 24 25 26 29 30 31 32 33 34 36 38 39 40 41 43 44 45 46 48 50 51 52 53 54 55 58 59 60 61 62 64 65 66 68 69 80 81 82 83 85 86 88 89 90 92 93 94 95 96 99 100

鸡兔同笼

public class Test{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("输入头:"); int head = scanner.nextInt(); System.out.println("输入脚:"); int feet = scanner.nextInt(); int chicken = 0, ribbit = 0, b = 0; if(feet == 2 * head ) { System.out.println("鸡的数量是:" + head); } else if(feet == 2 * head) { System.out.println("兔的数量是:" + head); } else { b = (feet - head * 2)/2; System.out.println("兔的数量是:" + b); System.out.println("鸡的数量是:" + (head - b)); } } }

输出结果为:

输入头: 2 输入脚: 8 兔的数量是:2 鸡的数量是:0

 

最新回复(0)