if选择结构

if选择结构

if单选

我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程用if语句来表示。

Scanner scanner = new Scanner(System.in);

       System.out.println("请输入内容");
       String s = scanner.nextLine();

       /*
       if单选结构
       if(布尔表达式){
          如果布尔表达式的值为true将执行的语句
       }
        */

       if (s.equals("hello")){
           System.out.println(s);
      }

       System.out.println("End");


       scanner.close();

 

if双选结构

案例:考试分数大于60分是及格,小于60分则不及格。

Scanner scanner = new Scanner(System.in);

System.out.println("请输入成绩:")
   int score = Scanner.nextInt();
   
   if(score>=60){
       System.out.println("及格")
  }else{
       System.out.println("不及格")
  }

scanner.close();

 

if多选结构

Scanner scanner = new Scanner(System.in);

System.out.println("请输入成绩")
   int score = Scanner.nextInt();

   if(score=100){
       System.out.println("恭喜满分")
  }else if(score<100 && score>=90){
       System.out.println("A级");
  }else if(score<90 && score>=80){
       System.out.println("B级");
  }else if(score<80 && score>=70){
       System.out.println("C级");
  }else if(score<70 && score>=60){
       System.out.println("D级");
  }else if(score<60 && score>=0){
       System.out.println("不及格");
  }else{
       System.out.println("非法输入");
  }
scanner.close();

 

posted @ 2021-04-27 10:42  牛油果柿子  阅读(34)  评论(0编辑  收藏  举报