Scanner object
Used to realize the interaction between programs and people, Java.util.Scanner is a new feature of java5, and we can get user input through the Scanner class.
Basic syntax:
Scanner s = new Scanner(System.in);
Get the input string through the next() and nextLine() methods of the Scanner class. Before reading, we generally need to use hasNext() " Is there a next one, with if statement " and hasNextLine() " Is there a next line? , With the if statement " to determine whether there is still input data.
import java.util.Scanner;、//导入Scanner包,自动
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner s =new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if (s.hasNext()==true){
//使用next方式接收
String str =s.next();//程序会等待用户输入完毕
System.out.println("输出的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源.要养成好习惯用完就关掉
s.close();
}
}
next():
Breaks if there is a space
-
1. You must read valid characters before you can end the input
-
2. For the blanks encountered before entering valid characters, the next() method will automatically remove them.
-
3. Only after valid characters are entered, the blanks entered after them are used as separators or terminators.
-
4. next() cannot get a string with spaces.
if (s.hasNext()==true){ //Use the next method to receive String str =s.next();//The program will wait for the user to input System.out.println("The output content is:"+str );
nextLine():
It will be broken if there is a carriage return
-
1. Take Enter as the terminator, which means that the nextLine() method returns all the characters before the carriage return.
-
2. You can get blanks.
if(scanner.hasNextLine()){ String str= scanner.nextLine(); //等待用户去输入 System.out.println("输出的内容为:"+str);
nextInt ():
Integer
End with a space
nextflout():
Float type
ends with a space
Example:
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
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();
}
}
Application questions:
We can enter multiple numbers, and find the sum and average. Each number is confirmed with Enter, and the input is terminated by entering a non-number and the execution result is output:
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果:
Scanner scanner =new Scanner(System.in);
double sum = 0;
//计算输入了多少个数字
int m= 0;
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m=m+1;
sum =sum + x;
System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum);
}
System.out.println(m+"个数的和为"+sum);
System.out.println(m+"个数的平均值是"+(sum/m));
scanner.close();
}
}