This question already exists:
Scanner issue when using nextLine after nextXXX [duplicate]

6年前关闭。





ask=1;
while(ask==1)
{
    System.out.println("What is your phrase?");
    phrase=sc.nextLine();
    System.out.println("What is your additive key?");
    key=sc.nextInt();
    String code=addit(phrase, key);
    System.out.println(code);
    System.out.println("Enter 1 to go again");
    ask=sc.nextInt();
}


跳过循环直接问我的附加密钥是什么,在第一次循环之后,有人知道为什么吗?

最佳答案

因此,根据@PakkuDon的说法,这应该可以解决问题:

ask = 1;
while(ask == 1)
{
    System.out.println("What is your phrase?");
    phrase = sc.nextLine();
    System.out.println("What is your additive key?");
    key = sc.nextInt();
    String code = addit(phrase, key);
    System.out.println(code);
    System.out.println("Enter 1 to go again");
    ask = sc.nextInt();
    String tmp = sc.nextLine();
}

09-25 16:25
查看更多