【问题标题】:Guessing game loop猜谜游戏循环
【发布时间】:2019-03-03 18:02:19
【问题描述】:

这是我制作的猜谜游戏的代码。我的计数器没有增加超过 1。我从单独的控制器类传递参数选择和生成的数字。 do while 循环是否应该在控制器类中?

            public String startGuessingGame(int choice, int generatedNumber) {

        int count = 0;
        final int attempts = 4;
        String result = null;

            do {

                count++;

                if (choice == generatedNumber) {

                    result = "Attempt " + count + " "
                            + "- You have guessed the correct number!";

                }

                else if (choice > 50 || choice < 0) {

                    result = "Out of range. "
                            + "\nPlease choose a number between 1 and 50."; 

                }

                else if (choice > generatedNumber) {

                    result = "Attempt " + count + " " 
                            + " - You have guessed too high!";

                }

                else if (choice < generatedNumber) {

                    result = "Attempt " + count + " " 
                            + "- You have guessed too low!";

                }


                if (count == attempts) {

                    result = "You are out of guesses! The number was " + generatedNumber;

                }
            }

        while(count < attempts);

            return result;

    }
}

【问题讨论】:

  • 这里实际上没有循环。我在任何地方都没有看到 for、while 或 do-while 语句。
  • 没有循环,就没有重复。你必须有一个循环。您应该确保检查循环中的正确条件。
  • 用循环发布你拥有的代码,因为你肯定需要一个循环,所以更有意义地帮助你
  • 抱歉,编辑的代码在上面
  • 我的错,我应该早点看到这个,你需要让你在方法startGuessingGame之外循环,或者让用户在方法内做出新的选择。现在将有一个选择,该方法将计数为 4(对于相同的选择)并退出。

标签: java loops counter


【解决方案1】:

这里没有循环。 您正在寻找类似while(count &lt; attempts) 的内容。

【讨论】:

    【解决方案2】:

    试试这个: 在while 条件之前的末尾递增计数器:

    do{
        ...
        if (count == attempts) {
    
            result = "You are out of guesses! The number was " + generatedNumber;
    
        }
        count++;
    }while(count < attempts);
    
    return result;
    
    ...
    

    【讨论】:

    • 这会导致代码直接跳转到“你没有猜到!”
    【解决方案3】:

    您需要将count 设为控制器类的类变量(成员),并在该类中设置do/while 循环,以便startGuessingGame 仅处理用户选择的验证。像这样,但代码远未完成

    public class SomeControllerClass() {
        final int attempts = 4;
        int count = 0;
    
        public void someMethod() {
            int choice = 0;
            do {
                choice = getChoice();
                count++;
                String text = otherClass.startGuessingGame(choice, generatedNumber);
            while (count < attempts);
        }
    

    并且该方法只进行验证

    public String startGuessingGame(int choice, int generatedNumber) {
        String result = null;
    
        if (choice == generatedNumber) {
            result = "Attempt " + count + " "
                            + "- You have guessed the correct number!";
        } else if (choice > 50 || choice < 0) {
        //and so on
       return result;
    }
    

    【讨论】:

    • 我需要将选项作为参数,因为它是从控制器类中获取的
    • @GearoidSheehan 好的,这里的一个问题是您的方法的行为就像它是控制器类的一部分,所以我想没有一个简单的解决方法。
    • @GearoidSheehan 我已经用新的解决方案更新了我的答案
    • @GearoidSheehan 对此有何反馈?
    • 我设法使用您的解决方案使计数正常工作,但是每次输出都是“您猜得太低了!”无论输入什么值。
    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2015-10-22
    • 2016-02-11
    相关资源
    最近更新 更多