Scanner 的用法

public class ScannerDemo {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入您的金钱");
        // s.hasNext() 是否有next()值?
        if(s.hasNext()){
            String str = s.next();
            System.out.println(str);
        }
        // s.hasNextLine() nextLine()值?
        // 重点: 1、s.next()不支持空格、空白
        //  2、s.nextLine()支持空白、空格  === 》 常用
        if(s.hasNextLine()){
            String str = s.nextLine();
            System.out.println(str);
        }

        if(s.hasNextInt()){
            int num = s.nextInt();
            System.out.println(num);
        }else{
            System.out.println("您输入的不是整数");
        }
        // IO流一定要随手关闭 ★
        s.close();
    }
}

  

posted @ 2021-02-11 20:22  火鸡的呐喊  阅读(95)  评论(0)    收藏  举报