Java编号猜测程序问题

时间:2015-03-28 04:19:14

标签: java

我有一个开始的Java程序,我一直致力于创建一个数字猜测程序,其中计算机猜测用户在一定范围内想到的数字。 我到目前为止已经有了下面的程序,但需要帮助摆脱一些扭结/添加功能。 - 首先,我需要进行设置,以便提示用户并给出范围。它应该运行并产生这样的东西: 欢迎来到这个有史以来第一个思维猜测计划!

请选择范围(大于1且不大于50):32

您将选择1到32之间的数字......我会尝试猜测它。

根据我的每一个猜测,你会告诉我我是否太高(h或H), 太低(l或L),匹配(m或M),或者你想退出(q或Q)。我的 目标是尽可能少地猜测数字。

- 第二,游戏应该放弃并在失败之​​后重新启动,猜测,但由于某种原因,在第五次失败后,它再次提示第五次猜测,然后重新启动后 - 我需要防止这种情况,所以看起来应该是这样的: 我的第四个猜测是17:h 我的猜测太高了?

我的第五个猜测是16:h ***这轮我很不走运。我放弃了。

让我们玩吧!

我的第一个猜测是10: 等。

import java.util.*;
import java.lang.Math;

public class numguessprac1 {

    // Declaring variables
    public static String input;
    public static int    quit;
    public static int    guess;
    public static int    wins;
    public static int    guessNum;
    public static int    losses;

    public static void main(String[] args) {

        System.out.println("Welcome to my first-ever mind-reading     program!\n");
        System.out.println("You will choose a number between 1 and 32... and    I will try to guess it.");
        System.out.println();
        System.out.println("With each of my guess, you will tell me whether     I am too high (h or H),\n" +
        "too low (l or L), match (m or M), or you want to quit (q or Q).    My\n" +
        "objective is to find the number using as few guesses as     possible.");   

        int totalGuessNum = 0;

        while (quit != 1) {
            System.out.println();
            gamePlay( numbersAsWords() );
            totalGuessNum =+ guessNum;
        } // Loop end

        printStats(totalGuessNum);

    } // Main end

    public static int gamePlay( String numbersAsWords ) {

        int countdown;
        Scanner s = new Scanner(System.in);
        Random r  = new Random();
        guess     = r.nextInt(2) + 16;  

        System.out.println("Let's play!");
        System.out.println();
        System.out.print("My " + numbersAsWords() + " guess is " + guess +     ": ");
        String input = s.nextLine();

        for (countdown = 3; countdown >= -1; countdown--) {

            if (countdown == -1 && !input.equals("M") && !input.equals("m"))     {
                System.out.println("*** I am unlucky this round. I give     up.");
                losses++;
                break;
            } else if
            (input.equals("Q") || input.equals("q")) {
                System.out.println("*** You quit!");
                quit = 1;
                break;
            } else if
            (input.equals("M") || input.equals("m")) {
                System.out.println("*** Correct!");
                guessNum++;
                wins++;
                break;
            } else if
            (input.equals("H") || input.equals("h")) {
                System.out.println("My guess is too high?");
                guessNum++;
                guess = guess - (int) Math.pow(2, countdown);
                System.out.println();
                System.out.print("My " + numbersAsWords() + " guess is " +     guess + ": ");
            } else if
            (input.equals("L") || input.equals("l")) {
                System.out.println("My guess is too low?");
                guessNum++;
                guess = guess + (int) Math.pow(2, countdown);
                System.out.print("\nMy " + numbersAsWords() + " guess is " +        guess + ": ");
            } else if (!input.equals("M")) {
                System.out.println("Sorry I do not understand your    response.\n");
                System.out.print("My " + numbersAsWords() + " guess is " +    guess + ": ");
                countdown++;
            }

            input = s.nextLine();

        } // End for loop

        return quit;

    } // End of playGame

    public static void printStats(int totalGuessNum) {

        double ratio;
        int    totalGames = wins + losses;

        System.out.println();
        System.out.println("My overall results:");
        System.out.println("Total games\t\t = " + totalGames);
        System.out.println("Succeeded games\t\t = " + wins);

        if (wins != 0) {
            ratio = (double) totalGuessNum / (double) wins;
            System.out.printf("Guesses/succ. game:\t = %1.1f\n", ratio);
        }
        else if (wins == 0) {
            ratio = 0;
            System.out.println("Guesses/succ. game:\t = " + ratio);
        }

        System.out.println("Gave-up games\t\t = " + losses);

    } // End of smartGuessMeth

    public static String numbersAsWords() {

        String ordinalWord = "first";
        switch (guessNum) {
            case 1: ordinalWord = "second";   break;
            case 2: ordinalWord = "third";    break;
            case 3: ordinalWord = "fourth";   break;
            case 4: ordinalWord = "fifth";    break;

        }
        return ordinalWord;

    } // 

} // End of class

1 个答案:

答案 0 :(得分:0)

您尚未重置计数器guessNum

if (countdown == -1 && !input.equals("M") && !input.equals("m"))     {
                System.out.println("*** I am unlucky this round. I give     up.");
                losses++;
                guessNum = 0;
                break;

会做的伎俩。