用户通过6个随机数猜测后,游戏仍然剩下1个

时间:2015-04-27 14:29:25

标签: java

该程序在1到49之间生成六个随机数,并将它们放入一个数组中 - 这些是获胜数字。要求用户猜测其中一个号码,每次他们猜测应用程序应该告诉用户这是否是中奖号码。

它没有像预期的那样工作,经过所有数字后仍有一个可以猜测,即使我通过所有数字,任何帮助将不胜感激

public class Lotterator {

    public static int[] populate ()
    {
        int[] winNums = new int[6];


        winNums[0] = (int)(Math.random() * 49  + 1);

        while (winNums[1] == winNums[0]){
        winNums[1] = (int)(Math.random() * 49  + 1);
        }

        while (winNums[2] == winNums[1] || winNums[2] == winNums[0]){
        winNums[2] = (int)(Math.random() * 49  + 1);
        }

        while (winNums[3] == winNums[2] || winNums[3] == winNums[1] || winNums[3] == winNums[0]){
        winNums[3] = (int)(Math.random() * 49  + 1);
        }

        while (winNums[4] == winNums[3] || winNums[4] == winNums[2] || winNums[4] == winNums[1] || winNums[4] == winNums[0]){
        winNums[4] = (int)(Math.random() * 49  + 1);
        }

        while (winNums[5] == winNums[4] || winNums[5] == winNums[3]|| winNums[5] == winNums[2]|| winNums[5] == winNums[1]|| winNums[5] == winNums[0]){
        winNums[5] = (int)(Math.random() * 49  + 1);
        }

        return winNums;
    }

    public static boolean[] checkUserNum (int[] winNums, int userNum)
    {
        boolean[] correctNum = new boolean[6];


        for(int c = 0;c < winNums.length; c++)
        {
            switch (c)
            {
            case 0:
                if (winNums[0] == userNum) {
                    correctNum[0] = true;
                }
                break;
            case 1: 
                if (winNums[1] == userNum) {
                    correctNum[1] = true;
                }
                break;
            case 2: 
                if (winNums[2] == userNum) {
                    correctNum[2] = true;
                }
                break;
            case 3: 
                if (winNums[3] == userNum) {
                    correctNum[3] = true;
                }
                break;
            case 4: 
                if (winNums[4] == userNum) {
                    correctNum[4] = true;
                }
                break;
            case 5: 
                if (winNums[5] == userNum) {
                    correctNum[5] = true;
                }
                break;
            }
        }

        if (userNum != winNums[0] && userNum != winNums[1] && userNum != winNums[2] && userNum != winNums[3] && userNum != winNums[4] && userNum != winNums[5])
        {
            TextIO.put("That’s not one of my numbers!");

        }
        else if (userNum == winNums[0] || userNum == winNums[1] || userNum == winNums[2] || userNum == winNums[3] || userNum == winNums[4] || userNum == winNums[5])
        {
            TextIO.put("You got one of my numbers!");

        }


        return correctNum;
    }

    public static int[] removeFromArray(boolean[] correctNum, int[] winNums)
    {

        for (int c = 0; c < winNums.length; c++)
        {
            if (correctNum[c] == true) {
                winNums[c] = 0;
            }

        }

        return winNums;
    }

    public static int getUserNum()
    {
        int userNum = 0;

        TextIO.put("Please input a number from 1 to 49:");
        do {    
        userNum = TextIO.getInt();
        } while(userNum < 1 || userNum > 49);

        return userNum;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        TextIO.putln("I bet you can’t guess my six numbers!\n");

        int userNum = 0;
        int[] winNums = new int[6];
        boolean[] correctNums = new boolean[6];
        int guesses = 0;
        int correctGuesses = 0;

        winNums = populate();

        do{
        userNum = getUserNum();
        guesses++;
        correctNums = checkUserNum(winNums, userNum);
        winNums = removeFromArray(correctNums, winNums);

        for(int c = 0; c < correctNums.length; c++)
        {
            if (correctNums[c] == true){
                correctGuesses++;
            }   
        }

        TextIO.putln(" You’ve guessed "+ correctGuesses + " out of "+ guesses + " correctly.\n");


        }while (correctGuesses != 6);


}
}

1 个答案:

答案 0 :(得分:1)

%d

在您的代码中,您正在检查数字0和1的while循环中的相等性。由于数字0已设置,因此它不相等。您可以在do-while循环中切换while循环然后它应该工作。当您检查阵列时,您会看到1号是零。因此总是留下一个数字。