首页 > 解决方案 > Java边做边猜游戏

问题描述

因此,对于上周我必须完成的作业,我必须使用 4 个 do-while 循环和 if 语句在 Java 中进行猜谜游戏。我无法成功完成它,班级继续前进,没有给我任何帮助。如果有人可以查看我的代码并告诉我可以在哪里改进它以便程序正常运行,我将不胜感激。

为了简要描述赋值,赋值调用了 4 个 do-while 循环:

  1. 包含大部分代码并保持程序运行直到用户想要退出的主要 do-while 循环
  2. 游戏 do-while 循环保持游戏运行,直到用户猜到正确的数字,此时它将退出。(这部分我想不通)。
  3. 游戏循环内的数字输入验证 do-while 循环,确保用户的猜测是有效的。
  4. 一个非数字输入验证 do-while 循环,它在游戏循环之后和之外,询问用户是否想再次玩,并检查有效的“Y”或“N”响应

这是我想出的:

package week4;

//Imports
import java.lang.Math;
import java.util.Scanner;


public class Lab4d {
    public static void main(String[] args) {

        //Set up scanners
        Scanner again = new Scanner(System.in);
        Scanner num1 = new Scanner(System.in);

        //Set up variables
        int userInput = 0;
        int guesses = 0;
        int min = 1;
        int max = 100;
        int range = max - min + 1;
        int randNum = (int)(Math.random() * range) + min;
        boolean valid = false;

        //Outside loop
        do{
            //Describe the game
            System.out.println("\nThis program is a guessing game.");
            System.out.println("\nThe computer will pick a random number "
                               + "between 1 and 100.");
            System.out.println("\nYou will try to guess it.");
            System.out.println("\nLet's play!");

            //Insert Game loop here
            do {
                System.out.println("\nI'm thinking of a number " +
                                   "between 1 and 100.");
                //Insert valid guess checker here
                do {
                    System.out.println("Please enter your guess: ");
                    if (num1.hasNextInt()){
                        userInput = num1.nextInt();
                        valid = true;
                    }
                    else {
                        System.out.println("Error: Please enter a whole number.\n");
                        num1.nextLine();
                    }
                }while(!valid);

                if (userInput > randNum) {
                    System.out.println("\nToo high!");
                    guesses++;
                }
                else if (userInput < randNum) {
                    System.out.println("\nToo Low!");
                    guesses++;
                }
                else if (userInput == randNum) {
                    System.out.println("You got it!");
                    System.out.println("It took you" + guesses + "tries");
                    valid = true;
                }
            }while(!valid);


            //Insert play again checker
            do {
                System.out.println("\nDo you want to play again?");
                System.out.println("\nEnter 'Y' if yes and 'N' if no.");
                String play = again.nextLine();
                if (play.equalsIgnoreCase("Y")) {
                    valid = true;
                }
                else if (play.equalsIgnoreCase("N")) {
                    valid = true;
                }

                else {
                    System.out.println("Error: Please answer with 'Y' or 'N'");
                }
            }while(!valid);

        }while(!valid);
    }
}

感谢您的帮助!

标签: java

解决方案


您的问题出在程序的这一部分

//Insert Game loop here
        do {
            System.out.println("\nI'm thinking of a number " +
                    "between 1 and 100.");
            //Insert valid guess checker here
            do {
                System.out.println("Please enter your guess: ");
                if (num1.hasNextInt()){
                    userInput = num1.nextInt();
                    valid = true; //>>>>> HERE YOU SET VALID TO TRUE 
                }
                else {
                    System.out.println("Error: Please enter a whole number.\n");
                    num1.nextLine();

                }

            }while(!valid);
            if (userInput > randNum) {
                System.out.println("\nToo high!");
                guesses++;
            }
            else if (userInput < randNum) {
                System.out.println("\nToo Low!");
                guesses++;
            }
            else if (userInput == randNum) {
                System.out.println("You got it!");
                System.out.println("It took you" + guesses + "tries");
                valid = true;
            }

        }while(!valid); //>>>>> AND THIS !VALID CHECK USES THE SAME "valid" boolean

所以只需像这样使用两个单独的布尔值

//Imports
import java.util.Scanner;

public class Lab4d{
public static void main(String[] args) {

    // Set up scanners
    Scanner again = new Scanner(System.in);
    Scanner num1 = new Scanner(System.in);

    // Set up variables
    int userInput = 0;
    int guesses = 0;
    int min = 1;
    int max = 100;
    int range = max - min + 1;
    int randNum = (int) (Math.random() * range) + min;
    boolean valid = false;

    // Outside loop
    do {
        // Describe the game
        System.out.println("\nThis program is a guessing game.");
        System.out.println("\nThe computer will pick a random number " + "between 1 and 100.");
        System.out.println("\nYou will try to guess it.");
        System.out.println("\nLet's play!");

        // Insert Game loop here
        do {
            System.out.println("\nI'm thinking of a number " + "between 1 and 100.");
            // Insert valid guess checker here

            boolean userInputValid = false;
            do {
                System.out.println("Please enter your guess: ");
                if (num1.hasNextInt()) {
                    userInput = num1.nextInt();
                    userInputValid = true;
                } else {
                    System.out.println("Error: Please enter a whole number.\n");
                    num1.nextLine();
                }
            } while (!userInputValid);

            if (userInput > randNum) {
                System.out.println("\nToo high!");
                guesses++;
            } else if (userInput < randNum) {
                System.out.println("\nToo Low!");
                guesses++;
            } else if (userInput == randNum) {
                System.out.println("You got it!");
                System.out.println("It took you" + guesses + "tries");
                valid = true;
            }

        } while (!valid);

        // Insert play again checker
        do {
            System.out.println("\nDo you want to play again?");
            System.out.println("\nEnter 'Y' if yes and 'N' if no.");
            String play = again.nextLine();
            if (play.equalsIgnoreCase("Y")) {

                valid = true;
            }

            else if (play.equalsIgnoreCase("N")) {

                valid = true;
            }

            else {
                System.out.println("Error: Please answer with 'Y' or 'N'");
            }

        } while (!valid);

    } while (!valid);
}

}

就像一个信息(与您的问题没有直接关系),小心将整数与“==”进行比较,它仅适用于从 -128 到 127 的值。(但您可以将 == 用于原始类型“int”)

https://wiki.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching


推荐阅读