Scanner 类的使用

/*
    Scanner类的使用:
        1.导类
        2.创建对象
        3.调用对应的方法获取数据
*/
import java.util.Scanner;//导入Scanner类

public class IfDemo4{
    public static void main(String[] args){
        //创建扫描器对象
        Scanner s = new Scanner(System.in);
        System.out.println("请输入分数:(0-100) ");
        
        //调用扫描器的方法接收从键盘上录入的数据
        int score = s.nextInt();//阻塞式方法.
        // String score = s.nextLine();//获取字符串
        if(score < 0){
            System.out.println("分数不能为负");
        }else if(score < 60){ //隐含条件 score>=0
            System.out.println("不及格");
        }else if(score < 90){ //隐含条件 score>=60
            System.out.println("良好");
        }else if(score < 100){ //隐含条件 score>=90
            System.out.println("优秀");
        }else if(score == 100){ //隐含条件 score>=100
            System.out.println("满分");
        }else{    //score>100
            System.out.println("分数非法");
        }
        
    }
}
原文地址:https://www.cnblogs.com/leo9257/p/8733277.html