首页 > 其他分享 >if语句

if语句

时间:2023-02-08 18:37:20浏览次数:31  
标签:语句 Scanner System println score scanner out

if单选结构

package test.scanner;
import java.util.Scanner;
public class demo06 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据:");
        String str = scanner.nextLine();
        if(str.equals("hello")){
            System.out.println(str);
        }
        System.out.println("End");
        scanner.close();
    }
}

if双选结构

package test.scanner;
import java.util.Scanner;
public class demo07 {
    public static void main(String[] args) {
        //输入大于等于60则输出及格,小于60不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据:");
        int str = scanner.nextInt();
        if(str >=60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }
        scanner.close();
    }
}

if多选结构

package test.scanner;
import java.util.Scanner;
public class demo08 {
    public static void main(String[] args) {
        //成绩在90-100输出优秀,80-90输出良好,80-60输出合格,60以下不合格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据:");
        int score = scanner.nextInt();
        if(score == 100){
            System.out.println("恭喜满分");
        }else if(score >=90 && score <100){
            System.out.println("良好");
        }else if(score >=80 && score <90){
            System.out.println("及格");
        }else if(score >=60 && score <80){
            System.out.println("合格");
        }else if(score >0 && score <60){
            System.out.println("不合格");
        }else{
            System.out.println("输入的成绩数据不合法");
        }
        scanner.close();
    }
}

标签:语句,Scanner,System,println,score,scanner,out
From: https://www.cnblogs.com/sara0618/p/17102904.html

相关文章