有 Java 编程相关的问题?

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

Java中的while loop同时返回祝贺和抱歉消息

我已经搜索了几个小时,不知道我做错了什么。这是我用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.

共 (3) 个答案

  1. # 1 楼答案

    嘿,你怎么知道你的循环退出是因为颜色是红色还是因为尝试已用尽。。。你需要把支票放进去。你的系统失效了

    系统。出来println(“对不起,+sName+”你已经超出了你的猜测范围。颜色是红色。”)

    需要退出while循环,并在while循环检查尝试是否用尽后立即处于if状态

  2. # 2 楼答案

    您应该执行以下操作:

            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.")
            }
    
  3. # 3 楼答案

    简单回答:当人们第一次猜错并且能够再次猜到时,即使人们此时猜对了,“对不起”输出消息仍然在这个while循环中。这意味着在第二次尝试中,人们猜对了,代码将执行“抱歉”打印,并结束while循环以执行“恭喜”输出