如何使用Scanner类从控制台读取输入?就像这样:
System.out.println("Enter your username: ");
Scanner = input(); // Or something like this, I don't know the code
基本上,我想要的只是让扫描器读取用户名的输入,并将输入分配给一个String变量。
如何使用Scanner类从控制台读取输入?就像这样:
System.out.println("Enter your username: ");
Scanner = input(); // Or something like this, I don't know the code
基本上,我想要的只是让扫描器读取用户名的输入,并将输入分配给一个String变量。
当前回答
有几种方法可以从用户那里获得输入。在这个程序中,我们将使用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
为了访问Scanner类中的方法,创建一个新的Scanner对象作为“in”。现在我们用它的一种方法,那就是“next”。“next”方法获取用户在键盘上输入的文本字符串。
这里我使用in.nextLine();来获取用户输入的字符串。
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);
}
}
其他回答
import java.util.*;
class Ss
{
int id, salary;
String name;
void Ss(int id, int salary, String name)
{
this.id = id;
this.salary = salary;
this.name = name;
}
void display()
{
System.out.println("The id of employee:" + id);
System.out.println("The name of employye:" + name);
System.out.println("The salary of employee:" + salary);
}
}
class employee
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Ss s = new Ss(sc.nextInt(), sc.nextInt(), sc.nextLine());
s.display();
}
}
你可以流动这段代码:
Scanner obj= new Scanner(System.in);
String s = obj.nextLine();
从控制台读取数据
BufferedReader is synchronized, so read operations on a BufferedReader can be safely done from multiple threads. The buffer size may be specified, or the default size(8192) may be used. The default is large enough for most purposes. readLine() « just reads data line by line from the stream or source. A line is considered to be terminated by any one these: \n, \r (or) \r\n Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace(\s) and it is recognised by Character.isWhitespace. « Until the user enters data, the scanning operation may block, waiting for input. « Use Scanner(BUFFER_SIZE = 1024) if you want to parse a specific type of token from a stream. « A scanner however is not thread safe. It has to be externally synchronized. next() « Finds and returns the next complete token from this scanner. nextInt() « Scans the next token of the input as an int.
Code
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);
}
流的输入和输出
Reader Input: Output:
Yash 777 Line1 = Yash 777
7 Line1 = 7
Scanner Input: Output:
Yash 777 token1 = Yash
token2 = 777
你可以在Java中使用Scanner类
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println("String: " + s);
有几种方法可以从用户那里获得输入。在这个程序中,我们将使用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
为了访问Scanner类中的方法,创建一个新的Scanner对象作为“in”。现在我们用它的一种方法,那就是“next”。“next”方法获取用户在键盘上输入的文本字符串。
这里我使用in.nextLine();来获取用户输入的字符串。
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);
}
}