简体   繁体   中英

Comparing integer in int array to a user provided integer

I have a two dimensional array, gridArray[5][7], populated with numbers (only the last row is populated with anything other than 0). I'm taking user input for a column and color, converting the color to a number representation, and then checking it against the rows in the array. If it's in the column the user enters, it should print "Your guess was correct". Instead, it seems to print "Your guess was incorrect", even if the guess was correct. I checked the boolean values, and it's returning false when it should be returning true. I can't figure out why. Here's my code:

public static void main(String[] args) {
        int[][] gridArray = new int[7][5];

        gridPop(gridArray);

        playFunc(gridArray);


    }

public static void playFunc(int[][] gridArray) {
    char colorGuess = 'N';
    int columnGuess = -1;
    String columnString = "a column to guess";
    boolean correctIncorrectGuess = false;

    System.out.println("Please enter a color to guess (R/B/P/Y/G)");
        colorGuess = charCheck();
            System.out.println("Please enter a column to guess");
            System.out.println("The column # must be between 1 and 5");
                columnGuess = intChecker(columnString);
                    correctIncorrectGuess = checkHat(gridArray, charReturn(colorGuess), columnGuess);
            System.out.println("Your guess was " + boolCorrectString(correctIncorrectGuess));
}

public static boolean checkHat(int[][] gridArray, int color, int column){
    boolean guess = false;
    for (int i = 0; i < gridArray.length; i++) {
        if (gridArray[i][column] == color) {
            guess = true;
        } else {
            guess = false;
        }
    }
    return guess;
}

public static int intChecker(String object) {
    boolean correctInput = false;
    int userInput = 0;

    while (!correctInput) {
        try {
            userInput = scanner.nextInt();
            correctInput = true;
        } catch(InputMismatchException e) {
            System.out.println("I'm sorry, that doesn't seem to be a number");
            System.out.println("Please enter " +object);
            scanner.next();
        } 
    }

    return userInput;
}

public static String boolCorrectString(boolean toString) {
    String correctInc = "";
        if (toString) {
            correctInc = "correct";
        } else {
            correctInc = "incorrect";
        }
        return correctInc;
}

public static int charReturn(char color) {
    int colorToInt = -1;
        if (color == 'G') {
            colorToInt =  1;
        } else if (color == 'R') {
            colorToInt =  2;
        } else if (color == 'B') {
            colorToInt =  3;
        } else if (color == 'P') {
            colorToInt =  4;
        } else if (color == 'Y') {
            colorToInt =  5;
        } else if (color == 'g') {
            colorToInt =  1;
        } else if (color == 'r') {
            colorToInt =  2;
        } else if (color == 'b') {
            colorToInt =  3;
        } else if (color == 'p') {
            colorToInt =  4;
        } else if (color == 'y') {
            colorToInt =  5;
        }
return colorToInt;
}

    public static char charCheck() {
        char userInput = 'N';

        userInput =  scanner.next().charAt(0);
            while((userInput != 'G') && (userInput != 'Y') && (userInput != 'B')
            && (userInput != 'R') && (userInput != 'P') && (userInput != 'g') && 
            (userInput != 'y') && (userInput != 'b') && (userInput != 'r') && (userInput != 'p')) {
                System.out.println("Please enter R/B/P/Y/G");
                    userInput =  scanner.next().charAt(0);
            }
            return userInput;
    }

I did some testing, and it's gathering the right numbers for the column, the representation of the color, etc. It seems that even when gridArray[i][column] should be the same as the guessed color, the boolean is being returned as false (I tested and it isnt printing incorrectly -- it's actually finding false when it should be true). Why isn't the code comparing the gridArray entry and the color guess correctly? Where does the code break down?

EDIT: Gridpop method

public static void gridPop(int[][] gridArray) {
    for (int i=0; i < gridArray[6].length; i++) {
        int randomNum = ThreadLocalRandom.current().nextInt(1, 6);
            gridArray[6][i] = randomNum;
    }
    for (int i=5; i >= 0; i--) {
        for (int j=0; j < gridArray[i].length; j++) {
            gridArray[i][j] = 0;
        }
    }
}

EDIT: I'm actually very very dumb. It was an off by one error, as it was checking at the user entered column, not the user entered column -1

Put a break after guess = true; and initialize int[][] gridArray with appropriate values; by default this array has zero in all its indexes. Here you are comparing zero with the color 's value which is non-zero. so it will always be false.

Your problem lies within the checkHat() method. When the matching element is first found, you set the boolean guess to true; however, when the next value checked is not equal to color guess is set back to false. Something like this should work:

public static boolean checkHat(int[][] gridArray, int color, int column){
    for (int i = 0; i < gridArray.length; i++) {
        if (gridArray[i][column] == color) {
            return true;
        }
    }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM