如何使用Scanner类从控制台读取输入?就像这样:

System.out.println("Enter your username: ");
Scanner = input(); // Or something like this, I don't know the code

基本上,我想要的只是让扫描器读取用户名的输入,并将输入分配给一个String变量。


当前回答

您可以编写一个简单的程序来询问用户的姓名,并打印任何回复使用的输入。

或者要求用户输入两个数字,您可以对这些数字进行加、乘、减或除,并像计算器一样打印用户输入的答案。

这里你需要Scanner类。你必须导入java.util.Scanner;,并且在你需要使用的代码中:

Scanner input = new Scanner(System.in);

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

看看这有什么不同:input.next();, i = input.nextInt();, d = input.nextDouble();

根据String类型,int类型和double类型的变化方式与其他类型相同。不要忘记代码顶部的import语句。

其他回答

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);
    }
}

你可以在Java中使用Scanner类

Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println("String: " + s);

下面是执行所需操作的完整类:

import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        final int valid = 6;

        Scanner one = new Scanner(System.in);
        System.out.println("Enter your username: ");
        String s = one.nextLine();

        if (s.length() < valid) {
            System.out.println("Enter a valid username");
            System.out.println(
                "User name must contain " + valid + " characters");
            System.out.println("Enter again: ");
            s = one.nextLine();
        }

        System.out.println("Username accepted: " + s);

        Scanner two = new Scanner(System.in);
        System.out.println("Enter your age: ");
        int a = two.nextInt();
        System.out.println("Age accepted: " + a);

        Scanner three = new Scanner(System.in);
        System.out.println("Enter your sex: ");
        String sex = three.nextLine();
        System.out.println("Sex accepted: " + sex);
    }
}

input.nextInt()方法有问题——它只读取int值。

因此,当使用input.nextLine()读取下一行时,您会收到“\n”,即回车键。所以要跳过这个,你必须添加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();

说明java.util.Scanner如何工作的一个简单示例是从System.in中读取单个整数。这真的很简单。

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

要检索用户名,我可能会使用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);

如果您想对输入进行更多控制,或者只是验证用户名变量,也可以使用next(字符串模式)。

你可以在java.util.Scanner的API文档中找到更多关于它们实现的信息