有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么要打印这个输出,似乎不合适

//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. 对的 什么东西越干越湿? 不正确。再试一次甚至在请求用户输入之前打印


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

    你必须总是在nextInt()之后做nextLine()。换行符没有被nextInt()吞没,因此下面的nextLine()吞没了它,而不是你想要的