Java 程序流程控制主要包括条件语句、循环语句和跳转语句。下面分别给出一些案例及答案:
一、分支结构
1. if条件语句
案例:判断一个整数是正数、负数还是零。
解析:使用 if-else 语句进行判断。
int num = 5;
if (num > 0) {
System.out.println("正数");
} else if (num < 0) {
System.out.println("负数");
} else {
System.out.println("零");
}
案例: 判断一个整数是否为偶数
解析:使用 if-else 语句进行判断
int num = 6;
if (num % 2 == 0) {
System.out.println("偶数");
} else {
System.out.println("奇数");
}
2. switch 条件语句
案例: 判断一个整数是奇数还是偶数
解析:使用 switch-case 语句进行判断。
int num = 6;
switch (num % 2) {
case 0:
System.out.println("偶数");
break;
default:
System.out.println("奇数");
}
案例: 根据输入的月份输出对应的季节
解析:使用 switch-case 语句进行判断。
int month = 3;
switch (month) {
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
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;
default:
System.out.println("输入错误");
}
案例: 根据输入的成绩输出对应的等级
解析:使用 switch-case 语句进行判断。
int score = 85;
switch (score / 10) {
case 10:
case 9:
System.out.println("优秀");
break;
case 8:
System.out.println("良好");
break;
case 7:
case 6:
System.out.println("及格");
break;
default:
System.out.println("不及格");
}
二、循坏结构
3.foi 循环语句
案例:输出 1 到 10 的和。
解析:使用 for 循环进行累加。
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("1 到 10 的和为:" + sum);
案例:输出 1 到 100 之间的所有素数。
解析:使用 for 循环遍历 1 到 100,再使用 for 循环判断每个数是否为素数。
public class PrimeNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime && i != 1) {
System.out.println(i);
}
}
}
}
案例:计算 1 到 100 之间的所有偶数之和
解析:使用 for 循环遍历 1 到 100,再使用 if 语句判断每个数是否为偶数,如果是则累加。
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
sum += i;
}
}
System.out.println("1 到 100 之间的所有偶数之和为:" + sum);
案例:输出九九乘法表
解析:使用嵌套的 for 循环实现。
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + (i * j) + "\t");
}
System.out.println();
}
4.while 循坏语句
案例:输出 1 到 10 之间的所有奇数
解析:使用 while 循环遍历 1 到 10,再使用 if 语句判断每个数是否为奇数。
int i = 1;
while (i <= 10) {
if (i % 2 != 0) {
System.out.println(i);
}
i++;
}
案例:计算 1 到 100 之间的所有偶数之和
解析:使用 while 循环遍历 1 到 100,再使用 if 语句判断每个数是否为偶数,如果是则累加。
int sum = 0;
int i = 1;
while (i <= 100) {
if (i % 2 == 0) {
sum += i;
}
i++;
}
System.out.println("1 到 100 之间的所有偶数之和为:" + sum);
5.do while 循坏语句
案例:输出 1 到 10 之间的所有奇数
解析:使用 do-while 循环遍历 1 到 10,再使用 if 语句判断每个数是否为奇数。
int i = 1;
do {
if (i % 2 != 0) {
System.out.println(i);
}
i++;
} while (i <= 10);
案例:输出九九乘法表
解析:使用嵌套的 do-while 循环实现。
int i = 1;
do {
int j = 1;
do {
System.out.print(j + " * " + i + " = " + (i * j) + "\t");
j++;
} while (j <= i);
System.out.println();
i++;
} while (i <= 9);
案例:计算阶乘
解析:使用 do-while 循环实现。
public class Factorial {
public static void main(String[] args) {
int num = 5;
int result = 1;
do {
result *= num;
n--;
} while (num > 1);
System.out.println(num + " 的阶乘为:" + result); //
}
}
三、跳转语句
案例:输出 1 到 10 之间的奇数。
解析:使用 for 循环遍历 1 到 10,然后使用 break 语句跳出循环。
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
以上就是今天课程案例啦 , 相信大家理解完以上案例 , 今天的内容就没问题啦 , 伙伴们都学会精通没有啊 !