Java学习-流程控制01用户交互Scanner
用户交互Scanner
Scanner对象
-
之前我们学习的基本语法中,我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入,java.util.Scanner是JDK5的新特性,我们可以通过Scanner类来获取用户的输入。
-
基本语法结构
Scanner s = new Scanner(System.in);
-
通过Scanner类的next()和nextLine()方法获取输入的字符串,在读取之前我们一般需要使用hasNext()和hasNextLine()判断是否还有输入的数据。
package com.struct.www; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); System.out.println("使用next方式接收:"); //判断用户有没有输入字符串 if(scanner.hasNext()){ //使用next方式接收数据 String str = scanner.next();//程序会等待用户输入完毕 System.out.println("输出的内容为:"+str); } //关闭扫描器,否则会一直占用资源 //凡是属于IO流的类不关闭都会一直占用资源,要养成好的习惯,用完就关掉 scanner.close(); } }以上代码运行结果
使用next方式接收: hello world 输出的内容为:hello用nextLine()进行接收
package com.struct.www; import java.util.Scanner; public class Demo02 { public static void main(String[] args) { //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); System.out.println("使用nextLine方式接收:"); //判断用户有没有输入字符串 if(scanner.hasNextLine()){ //使用next方式接收数据 String str = scanner.nextLine();//程序会等待用户输入完毕 System.out.println("输出的内容为:"+str); } scanner.close(); } }以上代码运行结果
使用nextLine方式接收: hello world 输出的内容为:hello world为什么next()和nextLine()运行结果会有区别?
- next()
- 1.一定要读取到有效字符后才会结束输入。
- 2.对输入的有效字符之前遇到的空白,next()方法会自动去除空白部分。
- 3.只有输入有效字符之后才将其后面输入的空白作为分隔符或者结束符。
- 4.next()不能得到带有空格的字符串。
- nextLine()
- 1.以Enter为结束符,也就是说nextLine()方法返回的是输入回车键之前的所有字符。
- 2.可以获取空白。
实际开发中基本都用nextLine()方法获取用户输入信息。
实际开发中的简洁写法
package com.struct.www; import java.util.Scanner; public class Demo03 { public static void main(String[] args) { //从键盘接收数据 Scanner scanner = new Scanner(System.in); System.out.println("请输入数据:"); String str = scanner.nextLine(); System.out.println("输出内容为:"+str); scanner.close(); } }拓展
package com.struct.www; import java.util.Scanner; public class Demo04 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //从键盘接收数据 int i = 0; float f = 0.0f; System.out.println("请输入整数:"); //如果...那么... if(scanner.hasNextInt()){ i = scanner.nextInt(); System.out.println("整数数据:"+i); }else{ System.out.println("输入的不是整数"); } //如果...那么... if(scanner.hasNextFloat()){ f = scanner.nextFloat(); System.out.println("小数数据:"+f); }else{ System.out.println("输入的不是小数"); } scanner.close(); } }以上代码运行结果
请输入整数: 12 整数数据:12 1.2 小数数据:1.2请输入整数: 1.2 输入的不是整数 小数数据:1.2 - next()
如果对Scanner实现感兴趣,可以去查看源码以及用IDEA查看Scanner的结构。
小案例
package com.struct.www;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车进行确认,通过输入非数字来结束并输出执行结果。
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计数器
int count = 0;
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while(scanner.hasNextDouble()){
double num = scanner.nextDouble();
count++;
sum += num;
System.out.println("你输入了第"+count+"个数据,然后当前的结果为:"+sum);
}
System.out.println(count + "个数的和为:"+sum);
System.out.println(count + "平均数为:"+(sum/count));
scanner.close();
}
}
以上代码运行结果
1
你输入了第1个数据,然后当前的结果为:1.0
2
你输入了第2个数据,然后当前的结果为:3.0
3
你输入了第3个数据,然后当前的结果为:6.0
4
你输入了第4个数据,然后当前的结果为:10.0
#
4个数的和为:10.0
4平均数为:2.5
浙公网安备 33010602011771号