Java Scanner类的详细介绍(Java键盘输入)


一、Scanner类的简单使用

Scanner类的功能,实现键盘输入数据

创建Scanner对象的基本语法:

Scanner sc = new Scanner(System.in); //System.in代表从键盘输入

简单使用

//由于Scanner类没有在java.lang包下,使用之前需要导包
import java.util.Scanner;

public class ScannerTest {
    
    
    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个int类型的数字:");
        int i = sc.nextInt();
        System.out.println("输入的数据为:"+i);

        System.out.println("请输入一个double类型的数字:");
        double d = sc.nextDouble();
        System.out.println("输入的数据为:"+d);

        System.out.println("请输入一个字符串:");
        String str = sc.next();
        System.out.println("输入的数据为:"+str);

        System.out.println("请输入一个布尔值:");
        boolean b = sc.nextBoolean();
        System.out.println("输入的数据为:"+b);
    }
}

程序运行结果:
在这里插入图片描述
【tips】
因为输入的数据已经赋值给变量了,所以可以对变量进行随意使用了。
使用Scanner类计算长方体的表面积与体积

import java.util.Scanner;

public class SurfaceAndVolume {
    
    

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入长方体的长:");
        double l = sc.nextDouble();
        System.out.println("请输入长方体的宽:");
        double w = sc.nextDouble();
        System.out.println("请输入长方体的高:");
        double h = sc.nextDouble();
        surface(l,w,h);
        volume(l,w,h);
    }

    public static void surface(double l,double w,double h){
    
    
        double s = (l*w+l*h+w*h)*2;
        System.out.println("长方体的表面积为:"+s);
    }

    public static void volume(double l,double w,double h){
    
    
        double v = l*w*h;
        System.out.println("长方体的体积为:"+v);
    }
}

二、Scanner类的详细介绍

1.判断输入数据类型

import java.util.Scanner;

public class ScannerJudge {
    
    

    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入:");
        //验证输入的数据类型
        if(sc.hasNextInt()) {
    
    
            int i = sc.nextInt();
            System.out.println("输入的数据类型为int,值为:"+i);
        } else if(sc.hasNextDouble()) {
    
    
            double d = sc.nextDouble();
            System.out.println("输入的数据类型为double,值为:"+d);
        } else if(sc.hasNextBoolean()) {
    
    
            boolean b = sc.nextBoolean();
            System.out.println("输入的数据类型为boolean,值为:"+b);
        } else if(sc.hasNext()) {
    
    
            String  str = sc.next();
            System.out.println("输入的数据类型为String,值为:"+str);
        }else {
    
    
            System.out.println("其他数据类型!");
        }
        sc.close();
    }
}

2.next()与nextLine()的区别

next():

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

nextLine():

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

对比两个方法的运行结果

import java.util.Scanner;

public class ScannerNextAndNextLine {
    
    

    public static void main(String[] args) {
    
    
        next();
        System.out.println("==============");
        nextLine();
    }

    //next方式接收数据
    public static void next(){
    
    
        Scanner sc = new Scanner(System.in);

        System.out.println("输入数据:(next方式接收)");
        if (sc.hasNext()){
    
    
            String str = sc.next();
            System.out.println("输入的数据类型为String,值为:"+str);
        }
    }

    //nextLine方式接收数据
    public static void nextLine(){
    
    
        Scanner sc = new Scanner(System.in);

        System.out.println("输入数据:(nextLine方式接收)");
        if (sc.hasNextLine()){
    
    
            String str = sc.nextLine();
            System.out.println("输入的数据类型为String,值为:"+str);
        }
    }
}

执行结果为:
在这里插入图片描述
对于两个方法同样都是都输入Hello Scanner!
next()只接收到了Hello,Scanner!被自动删掉
nextLine()将Hello Scanner!全部接收

3.求多个数字的平均数

案例分析:

输入多个数字,并求其总和与平均数
每输入一个数字用回车确认
通过输入非数字来结束输入并输出执行结果

代码实现:

import java.util.Scanner;

public class ScannerSum {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);

        double sum = 0;
        int m = 0;

        System.out.print("请开始输入数据:"+'\n'+"+");
        while (sc.hasNextDouble()) {
    
    
            double x = sc.nextDouble();
            m = m + 1;
            sum = sum + x;
            System.out.print("+");
        }

        System.out.println(m + "个数的和为" + sum);
        System.out.println(m + "个数的平均值是" + (sum / m));
        sc.close();
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_44580492/article/details/105867431
今日推荐