在Java编程中,条件判断是编程语言的核心特性之一。它允许程序根据不同的条件执行不同的代码块。通过解决以下30个实战练习题,你将能够加深对Java条件判断机制的理解,并提升你的编程智慧。
1. 基础条件判断
public class BasicCondition {
public static void main(String[] args) {
int number = 10;
if (number > 5) {
System.out.println("Number is greater than 5.");
} else {
System.out.println("Number is not greater than 5.");
}
}
}
解释:这个简单的例子检查一个整数是否大于5,并输出相应的消息。
2. 多条件判断
public class MultipleConditions {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b) {
System.out.println("a is greater than b.");
} else if (a < b) {
System.out.println("a is less than b.");
} else {
System.out.println("a and b are equal.");
}
}
}
解释:这个练习展示了如何使用多个条件进行判断。
3. 使用逻辑运算符
public class LogicalOperators {
public static void main(String[] args) {
int x = 10, y = 20;
if ((x > 5) && (y < 30)) {
System.out.println("Both conditions are true.");
} else {
System.out.println("One or both conditions are false.");
}
}
}
解释:逻辑运算符&&用于确保两个条件都必须为真。
4. 嵌套条件判断
public class NestedIf {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
if (number < 20) {
System.out.println("Number is between 0 and 20.");
} else {
System.out.println("Number is 20 or greater.");
}
} else {
System.out.println("Number is less than 0.");
}
}
}
解释:嵌套if语句用于处理更复杂的条件。
5. 使用三元运算符
public class TernaryOperator {
public static void main(String[] args) {
int a = 10, b = 20;
String result = (a > b) ? "a is greater" : "b is greater";
System.out.println(result);
}
}
解释:三元运算符提供了一种简洁的方式来执行简单的条件判断。
6. 检查变量是否为null
public class NullCheck {
public static void main(String[] args) {
String text = null;
if (text != null) {
System.out.println("The text is not null.");
} else {
System.out.println("The text is null.");
}
}
}
解释:这个例子演示了如何检查一个变量是否为null。
7. 检查字符串是否为空
public class StringEmptyCheck {
public static void main(String[] args) {
String str = "";
if (str.isEmpty()) {
System.out.println("The string is empty.");
} else {
System.out.println("The string is not empty.");
}
}
}
解释:使用isEmpty()方法检查字符串是否为空。
8. 检查数组长度
public class ArrayLengthCheck {
public static void main(String[] args) {
int[] array = {1, 2, 3};
if (array.length > 0) {
System.out.println("The array is not empty.");
} else {
System.out.println("The array is empty.");
}
}
}
解释:检查数组是否包含任何元素。
9. 检查集合是否为空
import java.util.ArrayList;
import java.util.List;
public class CollectionEmptyCheck {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
if (list.isEmpty()) {
System.out.println("The collection is empty.");
} else {
System.out.println("The collection is not empty.");
}
}
}
解释:检查一个集合是否为空。
10. 检查条件是否为真
public class BooleanCheck {
public static void main(String[] args) {
boolean condition = true;
if (condition) {
System.out.println("The condition is true.");
} else {
System.out.println("The condition is false.");
}
}
}
解释:检查一个布尔变量是否为真。
11. 使用switch语句
public class SwitchStatement {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Not a valid day.");
break;
}
}
}
解释:使用switch语句根据不同的值执行不同的代码块。
12. switch语句与字符串
public class SwitchWithString {
public static void main(String[] args) {
String color = "red";
switch (color) {
case "red":
System.out.println("The color is red.");
break;
case "blue":
System.out.println("The color is blue.");
break;
default:
System.out.println("The color is neither red nor blue.");
break;
}
}
}
解释:switch语句也可以用于字符串。
13. switch语句与枚举
public enum Weekday {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class SwitchWithEnum {
public static void main(String[] args) {
Weekday today = Weekday.WEDNESDAY;
switch (today) {
case MONDAY:
System.out.println("It's Monday.");
break;
case TUESDAY:
System.out.println("It's Tuesday.");
break;
case WEDNESDAY:
System.out.println("It's Wednesday.");
break;
// Other cases...
default:
System.out.println("It's not a weekday.");
break;
}
}
}
解释:switch语句也可以用于枚举类型。
14. 使用break语句
public class BreakStatement {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
}
}
解释:使用break语句退出循环。
15. 使用continue语句
public class ContinueStatement {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
}
}
解释:使用continue语句跳过当前循环的剩余部分。
16. 使用条件运算符
public class ConditionalOperator {
public static void main(String[] args) {
int x = 10, y = 20;
System.out.println((x > y) ? "x is greater" : "y is greater");
}
}
解释:条件运算符?:提供了一种简洁的方式来执行简单的条件判断。
17. 使用位运算符
public class BitwiseOperators {
public static void main(String[] args) {
int a = 5; // 101
int b = 3; // 011
int and = a & b; // 001
int or = a | b; // 111
int xor = a ^ b; // 110
int not = ~a; // 11011100
System.out.println("AND: " + and);
System.out.println("OR: " + or);
System.out.println("XOR: " + xor);
System.out.println("NOT: " + not);
}
}
解释:位运算符用于操作整数的二进制位。
18. 使用三元位运算符
public class TernaryBitwiseOperator {
public static void main(String[] args) {
int a = 5; // 101
int b = 3; // 011
System.out.println((a > b) ? a : b);
}
}
解释:位运算符也可以用于三元运算符。
19. 检查布尔数组
public class BooleanArrayCheck {
public static void main(String[] args) {
boolean[] flags = {true, false, true};
for (boolean flag : flags) {
if (flag) {
System.out.println("True found.");
} else {
System.out.println("False found.");
}
}
}
}
解释:检查布尔数组中的元素。
20. 检查整数数组
public class IntegerArrayCheck {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
}
解释:检查整数数组中的元素。
21. 检查字符串数组
public class StringArrayCheck {
public static void main(String[] args) {
String[] words = {"Java", "C++", "Python"};
for (String word : words) {
if (word.equals("Java")) {
System.out.println("Java found.");
} else {
System.out.println(word + " is not Java.");
}
}
}
}
解释:检查字符串数组中的元素。
22. 使用for循环和条件判断
public class ForLoopWithCondition {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
System.out.println(i + " is even.");
}
}
}
}
解释:在for循环中使用条件判断。
23. 使用while循环和条件判断
public class WhileLoopWithCondition {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
if (i % 2 == 0) {
System.out.println(i + " is even.");
}
i++;
}
}
}
解释:在while循环中使用条件判断。
24. 使用do-while循环和条件判断
public class DoWhileLoopWithCondition {
public static void main(String[] args) {
int i = 0;
do {
if (i % 2 == 0) {
System.out.println(i + " is even.");
}
i++;
} while (i < 10);
}
}
解释:在do-while循环中使用条件判断。
25. 使用if-else嵌套循环
public class IfElseNestedLoop {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j) {
System.out.println("Diagonal element: " + i + j);
} else {
System.out.println("Not diagonal element: " + i + j);
}
}
}
}
}
解释:在嵌套循环中使用if-else语句。
26. 使用switch语句和循环
public class SwitchWithLoop {
public static void main(String[] args) {
int day = 2;
switch (day) {
case 1:
case 2:
for (int i = 0; i < 5; i++) {
System.out.println("It's a weekend day.");
}
break;
default:
for (int i = 0; i < 5; i++) {
System.out.println("It's a weekday.");
}
break;
}
}
}
解释:在switch语句中使用循环。
27. 使用循环和条件判断查找最大值
public class FindMaxValue {
public static void main(String[] args) {
int[] numbers = {3, 5, 2, 8, 6};
int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
System.out.println("The maximum value is " + max);
}
}
解释:使用循环和条件判断找到数组中的最大值。
28. 使用循环和条件判断查找最小值
public class FindMinValue {
public static void main(String[] args) {
int[] numbers = {3, 5, 2, 8, 6};
int min = numbers[0];
for (int number : numbers) {
if (number < min) {
min = number;
}
}
System.out.println("The minimum value is " + min);
}
}
解释:使用循环和条件判断找到数组中的最小值。
29. 使用循环和条件判断计算平均值
public class CalculateAverage {
public static void main(String[] args) {
int[] numbers = {3, 5, 2, 8, 6};
int sum = 0;
for (int number : numbers) {
sum += number;
}
double average = (double) sum / numbers.length;
System.out.println("The average value is " + average);
}
}
解释:使用循环和条件判断计算数组中所有数字的平均值。
30. 使用循环和条件判断检查闰年
public class LeapYearCheck {
public static void main(String[] args) {
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
解释:使用循环和条件判断检查一个年份是否为闰年。
通过解决这些实战练习题,你将能够更深入地理解Java中的条件判断机制,并在实际编程中更加得心应手。祝你好运!
