Java Ask 字符串输入多个单词

Bra*_*ano 3 java

您好,我在使用 Java 询问直接全名时遇到问题。

System.out.println("DOCTOR");
System.out.println("Enter ID Number:");
idnumber = scan.next();
System.out.println("Enter Name");
name = scan.next();
System.out.println("Enter Field of Specialization:");
field = scan.next();

System.out.println("ID               " + idnumber);
System.out.println("Name             " + name);
System.out.println("Specializtion    " + field );
Run Code Online (Sandbox Code Playgroud)

当我在下面输入此信息时:

ID = 100 姓名 = 布兰登·苏拉诺

它给了我这个结果

ID               100    
Name             Brandon    
Specializtion    Sullano
Run Code Online (Sandbox Code Playgroud)

我希望名称是动态的,这样我就可以输入甚至两个单词,该怎么做?
提前致谢..

Wat*_*l0n 5

试试这个代码:

import java.util.Scanner;

public class DoctorDoctor {

    public static void main (String [] args) {

        int idnumber;
        String name, field;

        Scanner sc = new Scanner(System.in);

        System.out.println("DOCTOR");

        /** Assuming ID number is an integer value */
        System.out.print("Enter ID Number: ");
        idnumber = sc.nextInt();
        sc.nextLine(); // This is important, for clearing the new line character

        System.out.print("Enter Name: ");
        name = sc.nextLine();

        System.out.print("Enter Field of Specialization: ");
        field = sc.nextLine();

        System.out.println();
        System.out.printf("%-15s: %d%n", "ID Number", idnumber);
        System.out.printf("%-15s: %s%n", "Name", name);
        System.out.printf("%-15s: %s%n", "Specialization", field);

        sc.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

输入/输出示例:

DOCTOR
Enter ID Number: 100
Enter Name: Brandon
Enter Field of Specialization: Heart Surgeon

ID Number      : 100
Name           : Brandon
Specialization : Heart Surgeon
Run Code Online (Sandbox Code Playgroud)