2

我已经搜索了几个小时,无法弄清楚我做错了什么。这是我在 Java 中的第三个程序,我在作业中迷路了。我必须修改代码以向用户询问颜色,然后一次又一次地询问他们,直到他们猜到“红色”或者他们已经猜不到了。他们尝试了 4 次。我必须给他们反馈“你在 x 次尝试中猜对了”或“你用完了尝试。为了获得满分,我最多需要使用一个循环和一个条件。原始代码使用一段时间来比较颜色并给出无限猜测而不计算。

这是我到目前为止所拥有的,它确实可以编译。但是,如果第一次答案不正确,它会给我带来祝贺和抱歉的信息。如何在不使用另一个循环的情况下解决这个问题?

/*************************************************************************
 *  Compilation:  javac JavaLa2.java
 *  Execution:    java JavaLab3
 *  Mary Ross
 *  Date: April 12, 2011
 *
 *  % java JavaLab3
 * This program will ask the user for a color. It will ask until they guess “red” OR they have run out of guesses. 
 * They have four guesses.
 * Example:
 * What is your name? Jordan
 * What color do you guess? White
 * let’s try again: What color do you guess? red
 * “Congratulations! You guessed the correct color in 2 tries.”
 * 
 * NOTE: I have put a lot of comments to guide you in the code.
 *************************************************************************/

import java.io.*;

public class JavaLab3 {

    public static void main(String[] args) {
      // first we define our input streams.
      InputStreamReader input = new InputStreamReader(System.in);
      BufferedReader reader = new BufferedReader(input);   

      String sName ;
      String sColor;
      Integer numGuesses = 0;
      // we catch exceptions if some are thrown.
      try {
            System.out.println("what is your name?");
            sName = reader.readLine();  

            // then we will ask for the color
            System.out.println("What color do you guess?");
            sColor = reader.readLine();
            numGuesses = numGuesses +1;
            // at this point we have primed the condition for the while loop

            while (sColor.compareToIgnoreCase("red") != 0 && numGuesses < 4)  { 


                   System.out.println("You have guessed " + numGuesses + " times. You get four guesses.");
                   System.out.println("Let’s try again: What color do you guess?");
                   sColor = reader.readLine();
                   numGuesses = numGuesses + 1;

            System.out.println("Sorry, "  + sName + " you have exceeded your alloted guesses. The color is red.");


            }
           System.out.println("Congratulations! " + sName + " You correctly guessed Red in " + numGuesses + " tries.");
      } catch (IOException e){
            System.out.println("Error reading from user");
       }

    }

}

运行正确答案:

> what is your name?
 Mary
What color do you guess?
 red
Congratulations! Mary You correctly guessed Red in 1 tries.   

运行 1 个错误答案和正确答案:

what is your name?
 Mary
What color do you guess?
 white
You have guessed 1 times. You get four guesses.
Let’s try again: What color do you guess?
 red
Sorry, Mary you have exceeded your alloted guesses. The color is red.
Congratulations! Mary You correctly guessed Red in 2 tries.
4

7 回答 7

1

嘿,你怎么知道你的循环是因为颜色是红色还是因为尝试已经用尽而退出......你需要检查一下。你的系统输出

System.out.println("对不起," + sName + " 你超出了你的猜测范围。颜色是红色的。");

需要退出 while 循环并在 while 循环检查尝试是否用尽之后立即处于 if 条件。

于 2013-04-10T05:56:53.873 回答
1

You are printing the sorry message inside the while loop. So that will be printed everytime the loop runs. You need to move it outside. Also, your messages should be printed based on some condition.

You need to modify your while loop as:

while (sColor.compareToIgnoreCase("red") != 0 && numGuesses < 4)  { 

    System.out.println("You have guessed " + numGuesses + " times. You get four guesses.");
    System.out.println("Let’s try again: What color do you guess?");
    sColor = reader.readLine();

    // For incorrect guess, increase the `numGuesses`, and continue the loop
    numGuesses = numGuesses + 1;

}
if (numGuesses == 4) {
    // While loop breaks because of 2nd condition, Then print "Sorry"
    System.out.println("Sorry, "  + sName + " you have exceeded your alloted guesses. The color is red.");
} else {
    // 1st condition is satisfied. Correct color is guessed.
    System.out.println("Congratulations! " + sName + " You correctly guessed Red in " + numGuesses + " tries.");
}
于 2013-04-10T05:57:55.620 回答
0

简单的回答:当第一次猜错,又能猜对的时候,即使人猜对了,“Sorry”输出信息还在这个while循环中。这意味着在第二次尝试时,人们猜对了,代码将执行“抱歉”打印并结束 while 循环以执行“恭喜”打印。

于 2013-04-10T06:29:59.657 回答
0

您的代码中有这种行为:

while (sColor.compareToIgnoreCase("red") != 0 && numGuesses < 4)  { 


                   System.out.println("You have guessed " + numGuesses + " times. You get four guesses.");
                   System.out.println("Let’s try again: What color do you guess?");
                   sColor = reader.readLine();
                   numGuesses = numGuesses + 1;

            System.out.println("Sorry, "  + sName + " you have exceeded your alloted guesses. The color is red.");


            }
           System.out.println("Congratulations! " + sName + " You correctly guessed Red in " + numGuesses + " tries.");

“恭喜”消息始终while在您的循环之后打印。

你在循环之外需要这样的东西::

if (numGuesses < 4)  {
    System.out.println("Congratulations! " + sName + " You correctly guessed Red in " + numGuesses + " tries.");
} else {
    System.out.println("Sorry, "  + sName + " you have exceeded your allotted guesses. The color is red.");
}
于 2013-04-10T05:57:09.233 回答
0

您应该执行以下操作:

        System.out.println("What color do you guess?");
        boolean isCorrectGuess = false; 
        if(sColor.compareToIgnoreCase("red") == 0) {
            isCorrectGuess  = true;
        }
        sColor = reader.readLine();
        numGuesses = numGuesses +1;
        // at this point we have primed the condition for the while loop

        while (!isCorrectGuess && numGuesses < 4)  { 


               System.out.println("You have guessed " + numGuesses + " times. You get four guesses.");
               System.out.println("Let’s try again: What color do you guess?");
               sColor = reader.readLine();
               numGuesses = numGuesses + 1;
               if(sColor.compareToIgnoreCase("red") == 0) {
                  isCorrectGuess  = true;
               }
        }
        if(!isCorrectGuess)
            System.out.println("Sorry, "  + sName + " you have exceeded your alloted guesses. The color is red.");
        else
             System.out.println("Congratulations! " + sName + " You correctly guessed Red in " + numGuesses + " tries.")
        }
于 2013-04-10T06:00:42.640 回答
0

因为,您还没有进行任何类型的条件检查,所以恭喜 SOP,所以当 while 循环结束时,它就会移动到这个 SOP。您需要提及一个条件,它将为此提供某种控制流。我想类似的事情。

        if (sColor.compareToIgnoreCase("red")== 0 && numGuesses < 4) {
           System.out.println("Congratulations! " + sName + " You correctly guessed Red in " + numGuesses + " tries.");
        }

几个小贴士:

您可以切换到equalsIgnoreCase而不是使用compareToIgnoreCase

我在 if 条件中使用的内容,您也可以使用布尔标志,一旦获得正确的输入,将其切换为 true。

于 2013-04-10T06:05:23.140 回答
0

一些逻辑框架问题。

public static void main(String[] args) { 
        // first we define our input streams.
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input);

        String sName;
        String sColor;
        Integer numGuesses = 0;
        // we catch exceptions if some are thrown.
        try {
            System.out.println("what is your name?");
            sName = reader.readLine();

            // then we will ask for the color
//            System.out.println("What color do you guess?");
//            sColor = reader.readLine();
//            numGuesses = numGuesses + 1;
            // at this point we have primed the condition for the while loop

            while (numGuesses < 4) {
                System.out.println("What color do you guess?");
                sColor = reader.readLine();
                numGuesses = numGuesses + 1;
                if (sColor.compareToIgnoreCase("red") != 0) {
                    System.out.println("Sorry, " + sName + " you have exceeded your alloted guesses. The color is red.");

                    System.out.println("You have guessed " + numGuesses + " times. You get four guesses.");
//                    System.out.println("Let’s try again: What color do you guess?");

//                    sColor = reader.readLine();
//                    numGuesses = numGuesses + 1;

                } else {
                    System.out.println("Congratulations! " + sName + " You correctly guessed Red in " + numGuesses + " tries.");
                    break;
                }

            }
        } catch (IOException e) {
            System.out.println("Error reading from user");
        }

    }

输出1:

what is your name?
Kiran
What color do you guess?
a
Sorry, Kiran you have exceeded your alloted guesses. The color is red.
You have guessed 1 times. You get four guesses.
What color do you guess?
a
Sorry, Kiran you have exceeded your alloted guesses. The color is red.
You have guessed 2 times. You get four guesses.
What color do you guess?
a
Sorry, Kiran you have exceeded your alloted guesses. The color is red.
You have guessed 3 times. You get four guesses.
What color do you guess?
a
Sorry, Kiran you have exceeded your alloted guesses. The color is red.
You have guessed 4 times. You get four guesses.

输出2

what is your name?
Kiran
What color do you guess?
Pink
Sorry, Kiran you have exceeded your alloted guesses. The color is red.
You have guessed 1 times. You get four guesses.
What color do you guess?
red
Congratulations! Kiran You correctly guessed Red in 2 tries.
于 2013-04-10T06:09:04.587 回答