2

选择结构

使用if选择结构
  if (){
  }


  使用复杂条件下的if结构
  
   public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("请输入张浩的Java成绩:");        
    Scanner scanner = new Scanner(System.in);
    int score1 = scanner.nextInt();
    System.out.println("请输入张浩的音乐成绩:");
    int score2 = scanner.nextInt();
    if(score1>90&&score2>80){
        System.out.println("老师说:不错,奖励一个iphone5!");
    }   
    else if(score1==100&&score2>70){
        System.out.println("老师说:不错,奖励一个iphone5!");
    }

}

使用if-else选择结构

public static void main(String[] args) {
System.out.print(“刘珊珊的Java分数:”);
int score = input.nextInt();
if (score==100) {
System.out.println(“父亲给她买一辆车”);
}else if (score>=90){
System.out.println(“母亲给她买一部笔记本电脑”);
}else if (score>=60) {
System.out.println(“母亲给她买一部手机”);
}else if (score<60) {
System.out.println(“没有礼物”);

}

}

多重if选择结构
if (条件一){

}else if(条件二){

}else{
}
public static void main(String[] args) {

            Scanner input = new Scanner(System.in);

            System.out.println("请输入存款:");            //单位万元

            int money = input.nextInt();

            if (money>500) {

                    System.out.println("买凯迪拉克");

            }else if (money>100){

                    System.out.println("买帕萨特");

            }else if (money>50) {

                    System.out.println("买伊兰特");

           }else if (money>10) {

                    System.out.println("买奥拓");

          }else {

                    System.out.println("看来我只能买捷安特了");

        }

        }

}

嵌套if选择结构
if(条件1){
if(条件2){

}

}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(“请输入比赛成绩(s):”);
Scanner scanner=new Scanner(System.in);
int score = scanner.nextInt();
if(score<=10){
System.out.println(“请输入性别:”);
String sex = scanner.next();
if(“男”.equals(sex)){
System.out.println(“进入男子组决赛!”);
}
else if(“女”.equals(sex)){
System.out.println(“进入女子组决赛!”);
}
else{
System.out.println(“你输入的性别有误!”);
}

    }
    else{
        System.out.println("请再接再厉!");
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值