String clientName;
do
{
System.out.println("Enter your Name");
while (keyboard.hasNextInt())
{
System.out.println("That's not a name!");
keyboard.next();
System.out.println("Enter your Name");
}
clientName = keyboard.nextLine();
} while (clientName.equals(""));
任何想法为什么这段代码会出现“非法类型”,我正在把头发拉出来。
答案 0 :(得分:0)
我在这里看到的唯一问题是你没有初始化clientName
字符串。您应该使用""
初始化它。
以下是更正后的代码段:
public static void main (String[] args)
{
String clientName = "";
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Enter your Name: ");
while (keyboard.hasNextInt())
{
System.out.println("That's not a name!");
keyboard.next();
System.out.println("Enter your Name: ");
}
clientName = keyboard.nextLine();
} while (clientName.equals(""));
}