将int数组中的整数与用户提供的整数进行比较

时间:2017-12-06 05:27:11

标签: java arrays

我有一个二维数组gridArray [5] [7],填充了数字(只有最后一行填充了0以外的任何数据)。我将用户输入作为列和颜色,将颜色转换为数字表示,然后根据数组中的行进行检查。如果它在用户输入的列中,则应打印"您的猜测是正确的"。相反,它似乎打印"你的猜测不正确",即使猜测是正确的。我检查了布尔值,它应该返回true时返回false。我无法弄清楚原因。这是我的代码:

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;
    }

我做了一些测试,并且正在为列收集正确的数字,颜色的表示等等。似乎即使gridArray [i] [column]应该与猜测的颜色相同,布尔值被返回为假(我测试过它并没有错误地打印 - 当它应该是真的时它实际上发现了错误)。为什么没有正确比较gridArray条目和颜色猜测的代码?代码在哪里崩溃了?

编辑: Gridpop方法

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;
        }
    }
}
编辑:我实际上非常愚蠢。这是一个错误,因为它检查用户输入的列,而不是用户输入的列-1

2 个答案:

答案 0 :(得分:2)

break之后加guess = true;并使用适当的值初始化int[][] gridArray;默认情况下,此数组的所有索引都为零。在这里,您将零与color的非零值进行比较。所以它永远都是假的。

答案 1 :(得分:1)

你的问题在于checkHat()方法。首次找到匹配元素时,将boolean guess设置为true;但是,当检查的下一个值不等于颜色时,将猜测设置回false。这样的事情应该有效:

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;
}