zoukankan      html  css  js  c++  java
  • Java通过 Scanner 类来获取用户的输入

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

    import java.util.Scanner;

    Scanner s = new Scanner(System.in);// 从键盘接收数据  
    System.out.println("请输入:");
    int num=s.nextInt();//定义输入的类型为整数
    String str=s.nextLine();//定义输入的类型为字符串


    注释:next() 与 nextLine() 区别:
    next():

        1、一定要读取到有效字符后才可以结束输入。
        2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
        3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
        next() 不能得到带有空格的字符串。

    nextLine():

        1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
        2、可以获得空白。



    判断是否还有输入:
    格式:
          scan.hasNext数据类型
    例,
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入:");
            while(scan.hasNextInt()){   
              int num = scan.nextInt();
              System.out.println("输入的数据为:"+num);  
            }
            System.out.println("输入结束!");

  • 相关阅读:
    hrbust 1840 (树状数组第k大) 删点使用
    20150211--Smarty2-02
    20150211--Smarty2-01
    20150210--Smarty1-02
    20150210--Smarty1-01
    20150209--JS巩固与加强6-02
    20150209--JS巩固与加强6-01
    20150207--JS巩固与加强5
    20150206--JS巩固与加强4-02
    20150206--JS巩固与加强4
  • 原文地址:https://www.cnblogs.com/Downtime/p/7768622.html
Copyright © 2011-2022 走看看