Java 分支结构 if语句的使用
程序员文章站
2024-01-05 17:05:34
...
public class IfClass {
public static void main(String[] args) {
// if(boolean 表达式){语句块}
//boolean=true 执行语句块,反之不执行
int score=56;
if(score<60){
System.out.println("不及格");
}
//if(boolean 表达式){语句块1}else{语句块2}
//boolean=true 执行语句块1
//boolean=false执行语句块2
int score1=66;
if(score1<60){
System.out.println("不及格");
}else{
System.out.println("及格");
}
//基本数据类型判断是否相等用 ==
//char
char sex ='男';
if(sex=='男'){
System.out.println("请进男厕所");
}else{
System.out.println("请进女厕所");
}
//if(boolean 表达式){语句块1}else if(boolean 表达式){语句块2}else if(boolean 表达式){语句块3}....else{语句块n}
if(sex=='男'){
System.out.println("请进男厕所");
}else if(sex=='女'){
System.out.println("请进女厕所");
}else{
System.out.println("不进");
}
//声明三个变量取最大值:
int a = 10;
int b = 20;
int c = 3;
if(a>b){
if(a>c){
System.out.println("最大值为a");
}else{
System.out.println("最大值为c");
}
}else{
if(b>c){
System.out.println("最大值为b");
}else{
System.out.println("最大值为c");
}
}
//输入一个数字,输出对应的星期数
int weekday = 1;
if(weekday == 1){
System.out.println("星期一");
}else if(weekday == 2){
System.out.println("星期二");
}else if(weekday == 3){
System.out.println("星期三");
}else if(weekday == 4){
System.out.println("星期四");
}else if(weekday == 5){
System.out.println("星期五");
}else if(weekday == 6){
System.out.println("星期六");
}else if(weekday == 7){
System.out.println("星期天");
}else{
System.out.println("输入错误");
}
}
}