Do-While循环错误

时间:2016-02-05 13:44:44

标签: java

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(""));

任何想法为什么这段代码会出现“非法类型”,我正在把头发拉出来。

编辑:ALL MY CODE AS REQUESTED

1 个答案:

答案 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(""));
}