Hoc*_*yer 214 java java.util.scanner
我如何使用Scanner该类从控制台读取输入?像这样的东西:
System.out.println("Enter your username: ");
Scanner = input(); // Or something like this, I don't know the code
Run Code Online (Sandbox Code Playgroud)
基本上,我想要的只是让扫描程序读取用户名的输入,并将输入分配给String变量.
Run*_*tad 329
一个简单的例子来说明工作如何java.util.Scanner从中读取单个整数System.in.这真的很简单.
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
Run Code Online (Sandbox Code Playgroud)
要检索我可能会使用的用户名sc.nextLine().
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("Your username is " + username);
Run Code Online (Sandbox Code Playgroud)
next(String pattern)如果您想要更多地控制输入,或者只是验证username变量,您也可以使用.
您可以在API文档中找到有关其实现的更多信息java.util.Scanner
Mat*_*Bro 32
Scanner scan = new Scanner(System.in);
String myLine = scan.nextLine();
Run Code Online (Sandbox Code Playgroud)
Yas*_*ash 22
从控制台读取数据
BufferedReader是同步的,因此可以从多个线程安全地完成对BufferedReader的读取操作.可以指定缓冲区大小,或者可以使用默认大小(8192).对于大多数用途,默认值足够大.
readLine() « 只是从流或源中逐行读取数据.一行被认为是由以下任何一行终止的:\n,\ r \n(或)\ r \n
Scanner使用分隔符模式将其输入分解为标记,分隔符模式默认匹配空格(\ s)并由其识别Character.isWhitespace.
« 在用户输入数据之前,扫描操作可能会阻塞,等待输入. « 使用扫描仪(BUFFER_SIZE = 1024),如果你想从流分析特定类型的令牌. " 但扫描仪不是线程安全的.它必须在外部同步.
next()«查找并返回此扫描仪的下一个完整令牌.nextInt()«将输入的下一个标记扫描为int.
码
String name = null;
int number;
java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine(); // If the user has not entered anything, assume the default value.
number = Integer.parseInt(in.readLine()); // It reads only String,and we need to parse it.
System.out.println("Name " + name + "\t number " + number);
java.util.Scanner sc = new Scanner(System.in).useDelimiter("\\s");
name = sc.next(); // It will not leave until the user enters data.
number = sc.nextInt(); // We can read specific data.
System.out.println("Name " + name + "\t number " + number);
// The Console class is not working in the IDE as expected.
java.io.Console cnsl = System.console();
if (cnsl != null) {
// Read a line from the user input. The cursor blinks after the specified input.
name = cnsl.readLine("Name: ");
System.out.println("Name entered: " + name);
}
Run Code Online (Sandbox Code Playgroud)
Reader Input: Output:
Yash 777 Line1 = Yash 777
7 Line1 = 7
Scanner Input: Output:
Yash 777 token1 = Yash
token2 = 777
Run Code Online (Sandbox Code Playgroud)
小智 11
input.nextInt()方法存在问题 - 它只读取int值.
因此,当使用input.nextLine()读取下一行时,您会收到"\n",即Enter键.所以要跳过这个,你必须添加input.nextLine().
试试这样:
System.out.print("Insert a number: ");
int number = input.nextInt();
input.nextLine(); // This line you have to add (it consumes the \n character)
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();
Run Code Online (Sandbox Code Playgroud)
Jai*_*tel 10
有几种方法可以从用户那里获得输入.在这个程序中,我们将使用Scanner类来完成任务.这个Scanner类来自java.util,因此该程序的第一行是import java.util.Scanner; 它允许用户在Java中读取各种类型的值.import语句行应该在java程序的第一行,我们继续进行代码.
in.nextInt(); // It just reads the numbers
in.nextLine(); // It get the String which user enters
Run Code Online (Sandbox Code Playgroud)
要访问Scanner类中的方法,请将新扫描程序对象创建为"in".现在我们使用它的一种方法,即"下一步"."next"方法获取用户在键盘上输入的文本字符串.
这里我in.nextLine();用来获取用户输入的String.
import java.util.Scanner;
class GetInputFromUser {
public static void main(String args[]) {
int a;
float b;
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string " + s);
System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer " + a);
System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float " + b);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] arguments){
Scanner input = new Scanner(System.in);
String username;
double age;
String gender;
String marital_status;
int telephone_number;
// Allows a person to enter his/her name
Scanner one = new Scanner(System.in);
System.out.println("Enter Name:" );
username = one.next();
System.out.println("Name accepted " + username);
// Allows a person to enter his/her age
Scanner two = new Scanner(System.in);
System.out.println("Enter Age:" );
age = two.nextDouble();
System.out.println("Age accepted " + age);
// Allows a person to enter his/her gender
Scanner three = new Scanner(System.in);
System.out.println("Enter Gender:" );
gender = three.next();
System.out.println("Gender accepted " + gender);
// Allows a person to enter his/her marital status
Scanner four = new Scanner(System.in);
System.out.println("Enter Marital status:" );
marital_status = four.next();
System.out.println("Marital status accepted " + marital_status);
// Allows a person to enter his/her telephone number
Scanner five = new Scanner(System.in);
System.out.println("Enter Telephone number:" );
telephone_number = five.nextInt();
System.out.println("Telephone number accepted " + telephone_number);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以创建一个简单的程序来询问用户的姓名并打印回复使用输入的内容.
或者要求用户输入两个数字,您可以添加,乘法,减去或除以这些数字,并打印用户输入的答案,就像计算器的行为一样.
所以你需要Scanner类.您必须import java.util.Scanner;在您需要使用的代码中使用:
Scanner input = new Scanner(System.in);
Run Code Online (Sandbox Code Playgroud)
input 是一个变量名.
Scanner input = new Scanner(System.in);
System.out.println("Please enter your name: ");
s = input.next(); // Getting a String value
System.out.println("Please enter your age: ");
i = input.nextInt(); // Getting an integer
System.out.println("Please enter your salary: ");
d = input.nextDouble(); // Getting a double
Run Code Online (Sandbox Code Playgroud)
看到这种不同:input.next();,i = input.nextInt();,d = input.nextDouble();
根据String,int和a double以相同的方式改变其余部分.不要忘记代码顶部的import语句.
| 归档时间: |
|
| 查看次数: |
1528289 次 |
| 最近记录: |