java 选择结构if

if条件语句分为三种语法格式,每一种格式都有它自身的特点

1.if语句

if语句是指如果满足某种条件,就进行某种处理

公式:

if (条件语句){

   执行语句;

   ……

}

1 public class Pd{
2     public static void main(String[] args){
3         int time = 5;
4         if(time<7){
5             System.out.println("闹钟响了,该起床了!");
6         }
7     }
8 }

结果:会输出闹钟响了,该起床了!

2.if…else语句

if (判断条件){

   执行语句1

   ……

}else{

   执行语句2

   ……

}

 1 public class Pd {
 2 
 3     public static void main(String[] args) {
 4         int a = 4,b = 7; //定义两个int整型变量
 5         int max;
 6         if(a>b){
 7             max = a;
 8         }else{
 9             max = b;
10             System.out.println(max);
11         }
12     }      
13 }

结果:会输出7

3.if…else if…else语句

if (判断条件1) {

   执行语句1

} else if (判断条件2) {

   执行语句2

}

...

else if (判断条件n) {

   执行语句n

} else {

   执行语句n+1

}

 1 public class Pd{
 2     public static void main(String[] args){
 3         int score = 103;
 4         if(score>=90 && score<=100){
 5             System.out.println("优秀");
 6         }else if(score>=60 && score<90){
 7             System.out.println("合格");
 8         }else if(score>=0 && score<60){
 9             System.out.println("不及格");
10         }else{
11             System.out.println("错误或异常");
12         }
13     }
14 }

结果:会输出错误或异常

 

 

 

转载于:https://www.cnblogs.com/ty-v/p/8029033.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值