Java scanner详细

1.next( ),nextLine( ),nextInt( )的区别

使用 Scanner 读取字符串/整数/浮点数

  • nextInt( ):只读取整数数值,并在读取输入后把光标留在本行
  • nex( ):读取输入直到遇见空格。此方法不能读取被空格分隔开的内容,并在读取输入后把光标留在本行。
  • netLine( ):读取包括空格在内的输入,而且还会读取行尾的换行符/n,以Enter结束,读取完成后光标内被放在下一行

2.hastNext( ) ,hasNextLine( )的用法

方法判断当前是否有输入,当键盘有输入后执行循环

  Scanner scan = new Scanner(System.in);	//构造Scanner类的对象scan,接收从控制台输入的信息
        System.out.println("请输入你的姓名");
        String name = scan.nextLine();//接收一个字符串,可以加除Enter以外的所有符号,包括空格和Tab
        System.out.println("请输入你的ID");
        String ID ;
        while(scan.hasNextLine()) {// hasNextLine()方法判断当前是否有输入,当键盘有输入后执行循环
            if(scan.hasNextInt()) {// 判断输入的值是否为整数类型,当为整数类型时执行循环
                ID = scan.nextLine();
                System.out.println("你输入的姓名为:"+name);
                System.out.println("你输入的ID为:"+ID);
                break;
            }else {
                System.out.println("请输入数字哦!");
                ID = scan.nextLine();
                continue;
            }
        }

Scanner 循环读取n个数字

public class Test {
    public static void main(String[] args) {
        //使用Scanner循环读取n个数字
        System.out.println("请输入数字");
        Scanner  sc=new  Scanner(System.in);
        int c=0;
        int sum=0;
        while(sc.hasNextInt()){
            int  tmp=sc.nextInt();
            sum+=tmp;
              c++;
        }
        System.out.println(sum/c);
        sc.close();
        }
        }

Ctrl+z结束

3.代码演示

public class Test1 {
    public static void main(String[] args) {
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s1=sc.nextLine();
        System.out.print("请输入第二个字符串:");
        s2=sc.next();
        System.out.println("输入的字符串是:"+s1+" "+s2);

    }
}

结果:

请输入第一个字符串:majingni
请输入第二个字符串:hard work
输入的字符串是:majingni hard
public class Test1 {
    public static void main(String[] args) {
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s1=sc.next();
        System.out.print("请输入第二个字符串:");
        s2=sc.nextLine();
        System.out.println("输入的字符串是:"+s1+" "+s2);

    }
}

结果:

请输入第一个字符串:majingni
请输入第二个字符串:输入的字符串是:majingni 

double nextDouble() , float nextFloat() , int nextInt() 等与nextLine()连用时都存在这个问题,解决的办法是:在每一个 next()、nextDouble() 、 nextFloat()、nextInt() 等语句之后加一个nextLine()语句,将被next()去掉的Enter结束符过滤掉。

public class NextTest{  
    public static void main(String[] args) {  
        String s1,s2;  
        Scanner sc=new Scanner(System.in);  
        System.out.print("请输入第一个字符串:");  
        s1=sc.next();  
        sc.nextLine();
        System.out.print("请输入第二个字符串:");  
        s2=sc.nextLine();  
        System.out.println("输入的字符串是:"+s1+" "+s2);  
    }  
}  
public class Test1 {
    public static void main(String[] args) {
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s1=sc.next();
        sc.nextLine();
        System.out.print("请输入第二个字符串:");
        s2=sc.nextLine();
        System.out.println("输入的字符串是:"+s1+" "+s2);

    }
}

结果:

请输入第一个字符串:ma
请输入第二个字符串:jing ni
输入的字符串是:ma jing ni
  Scanner in = new Scanner(System.in);
                while (in.hasNext()) {
                    int n = in.nextInt();
        /* nextLine()是扫描器执行当前行,并返回跳过的输入信息,特别需要注意!!!

            如果没有该行,则执行第一个in.nextLine()命令时的返回值是int n = in.nextInt()的值*/
                    in.nextLine();
                    HashSet<String> set = new HashSet<String>();
                    for (int i = 0; i < n; i++) {
                        String line = in.nextLine();
                        String[] arr = line.split(" ");
                        for (int j = 0; j < arr.length; j++) {
                            set.add(arr[j]);
                        }
                    }
                    System.out.println("sum:" + set.size());

                }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值