📒博客首页:Sonesang的博客
🎉欢迎关注🔎点赞👍收藏⭐️留言📝
❤️ :热爱Java与算法学习,期待一起交流!
🙏作者水平很有限,如果发现错误,求告知,多谢!
🌺有问题可私信交流!!!
一、if 语句
1. 基本if-else语句
当条件成立时,执行某些语句;否则执行另一些语句。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a > 5) {
System.out.printf("%d is big!\n", a);
System.out.printf("%d + 1 = %d\n", a, a + 1);
} else {
System.out.printf("%d is small!\n", a);
System.out.printf("%d - 1 = %d\n", a, a - 1);
}
}
}
else 语句可以省略:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a > 5) {
System.out.printf("%d is big!\n", a);
System.out.printf("%d + 1 = %d\n", a, a + 1);
}
}
}
当只有一条语句时,大括号可以省略:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a > 5)
System.out.printf("%d is big!\n", a);
else
Sy

本文详细介绍了Java中的if-else语句、比较运算符的使用、条件表达式以及switch语句,通过实例演示了如何在编程中实现基本的逻辑控制。

4017

被折叠的 条评论
为什么被折叠?



