跳过while循环的第一行

时间:2014-03-06 04:08:11

标签: java while-loop

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

直接跳过询问我的添加键是在第一次通过循环之后是谁知道为什么?

1 个答案:

答案 0 :(得分:1)

所以,根据@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();
}