-1
//Question4 
correct = false;
System.out.println("how many feet are in a yard?");
while (!correct)
{
    x1 = input.nextInt();
    if (x1==3)
    {
        System.out.println("correct.");
        correct = true;
    }
    else
    {
        System.out.println("incorrect. try again.");
    }
}


    //Question5
correct = false;
System.out.println("What gets wetter and wetter the more it dries?");
while (correct == false)
{

   s1 = input.nextLine();
   if (s1.equalsIgnoreCase("a towel")) // cheating is bad!
    {
        System.out.println("correct");
        correct = true;
    }

    else
    {
        System.out.println("incorrect. try again.");
    }
}

ヤードに何フィートあるかを出力しますか?3正解。乾くほど、何が濡れて濡れますか? 正しくない。再試行。ユーザー入力を求める前でも印刷されます。

4

1 に答える 1

1

最初の質問を次のように変更します。

while (!correct)
{
    x1 = input.nextInt();
    input.nextLine(); //NEW CODE
    if (x1==3)
    {
        System.out.println("correct.");
        correct = true;
    }
    else
    {
        System.out.println("incorrect. try again.");
    }
}

必ず a のnextLine()後に aを実行する必要がありnextInt()ます。改行は に飲み込まれていないnextInt()ため、以下nextLine()はそれを飲み込んでおり、必要なものではありません。

于 2013-03-06T23:41:41.337 に答える