java - Java 中的 while 循环同时返回祝贺和抱歉消息

标签 java

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

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

/*************************************************************************
 *  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.

最佳答案

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

System.out.println("抱歉,"+ sName + "您已经超出了分配的猜测范围。颜色是红色。");

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

关于java - Java 中的 while 循环同时返回祝贺和抱歉消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15917950/

相关文章:

java - 如何在 SQL 查询中安全地使用 * 作为通配符

java - 如何统计Spring Boot应用程序中的请求数量?

java - python 中的 utf-8 字符串索引与 java 不兼容

java - 将 CSV 转换为 ORC 时出现异常

java - Android如何检查php消息以验证登录

java - 如何从上一个停止点下载数据?

java - 构建树状结构

java - 为什么我无法在 Android 的 SQLite 数据库中添加这些默认值?

java - 深度嵌套继承——好的还是坏的做法?

java - Google 云端点(应用程序引擎)+ oauth2 与 android 集成