zoukankan      html  css  js  c++  java
  • Java Scanner 类

    可以通过 Scanner 类来获取用户的输入。

    创建Scanner对象的基本语法:

    Scanner s = new Scanner(System.in);
    

    接下来我们演示一个最简单的数据输入,并通过 Scanner 类的 next()nextLine() 方法获取输入的字符串,在读取前我们一般需要 使用 hasNexthasNextLine 判断是否还有输入的数据:

    使用next方法

    示例:

    import java.util.Scanner;
    
    public class ScannerDemo {
        public static void main(String[] args){
            // 创建Scanner对象,从键盘输入
            Scanner s = new Scanner(System.in);
    
            // next方式接收
            System.out.println("以next方式接收:");
            if (s.hasNext()){
                String str = s.next();
                System.out.println("输入的数据为:"+ str);
            }
            s.close();
        }
    }
    

    运行:

    以next方式接收:
    cnblogs/youcoding com
    输入的数据为:cnblogs/youcoding
    
    Process finished with exit code 0
    

    可以看到 com 字符串并未输出,接下来我们看 nextLine。

    使用 nextLine 方法:

    示例:

    import java.util.Scanner;
    
    public class ScannerDemo {
        public static void main(String[] args){
            // 创建Scanner对象,从键盘输入
            Scanner s = new Scanner(System.in);
    
            // nextLine方式接收
            System.out.println("以nextLine方式接收:");
            if (s.hasNextLine()){
                String str = s.nextLine();
                System.out.println("输入的数据为:"+ str);
            }
            s.close();
        }
    }
    

    运行:

    以nextLine方式接收:
    cnblogs/youcoding com
    输入的数据为:cnblogs/youcoding com
    
    Process finished with exit code 0
    

    可以看到 com 字符串输出。

    next() 与 nextLine() 区别:

    next():

    • 对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。

    • 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。

    • next() 不能得到带有空格的字符串。

    nextLine():

    • 以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。

    • 可以获得空白。

    如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取:

    示例:

    import java.util.Scanner;
    
    public class ScannerDemo {
        public static void main(String[] args){
            // 创建Scanner对象,从键盘输入
            Scanner s = new Scanner(System.in);
    
            int i=0;
            float f = 0.0f;
            System.out.println("输入整数:");
            if (s.hasNextInt()){  //判断输入的是否是整数
                i = s.nextInt();  // 接收整数
                System.out.println("整数为:"+i);
            }else {
                System.out.println("输入的不是整数");
            }
    
            System.out.println("输入小数:");
            if (s.hasNextFloat()) { // 判断驶入的是否是小数
                f = s.nextFloat();  // 接收小数
                System.out.println("小数为:"+f);
            }else {
                System.out.println("输入的不是小数");
            }
            s.close();
        }
    }
    

    运行:

    输入整数:
    10
    整数为:10
    输入小数:
    1.2
    小数为:1.2
    
    Process finished with exit code 0
    

    每天学习一点点,每天进步一点点。

  • 相关阅读:
    css样式优先级
    combobox addobject 字符串
    转:delphi dpk编译 Error: E2161 RLINK32: Unsupported 16bit resource in file xxx 问题解决
    ansistring-->unionstring 怪码
    一定要牢记软件工程的铁律
    转:oracle 删除表空间错误 提示:ora-02429:无法删除用于强制唯一
    delphi7 string 转到 PWideChar 用于连接unicode dll调用
    delphi中调用 DLL一定要注意声明函数的大小写
    slinebreak、 raise用法
    idHttp 中GET POST应用
  • 原文地址:https://www.cnblogs.com/youcoding/p/12702987.html
Copyright © 2011-2022 走看看