2.if语句

If语句

1.普通If语句

package com.lin.study.ifyuju;

import java.util.Scanner;

public class IfDemo01 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入内容: ");

       String s = scanner.nextLine();

       //equals:判断字符串内容 是否相等
       if(s.equals("Hello")){
           System.out.println(s);
      }

       System.out.println("End");

       //关闭流
       scanner.close();
  }
}

运行结果:

请输入内容: Hello Hello End

Process finished with exit code 0

2.if...else语句

package com.lin.study.ifyuju;

import java.util.Scanner;

public class IfDemo02 {
   public static void main(String[] args) {
       //考试分数大于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();
  }
}

运行结果:

请输入您的分数: 99 您的成绩是及格!

Process finished with exit code 0

3.else if语句

package com.lin.study.ifyuju;

import java.util.Scanner;

public class IfDemo03 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);

       System.out.println("请输入您的成绩: ");
       int score = scanner.nextInt();

       if(score==100){
           System.out.println("恭喜你!获得了满分!");
      }else if(score>=80&&score<100){
           System.out.println("A级,非常优秀!");
      }else if(score>=60&&score<80){
           System.out.println("B级,值得肯定!");
      }else if (score<60){
           System.out.println("C级,没有及格,请继续努力!");
      }else{
           System.out.println("分数不合法,请重新输入!");
      }

       //关闭流
       scanner.close();
  }
}

运行结果:

请输入您的成绩: 99 A级,非常优秀!

Process finished with exit code 0

posted @ 2021-10-06 22:05  木木9_9  阅读(170)  评论(0)    收藏  举报