简体   繁体   English

JFormattedTextField和Integer对象之间的比较

[英]Comparison between the JFormattedTextField and Integer objects

I have somewhat of a strange problem. 我有些奇怪的问题。

I'm writing a simple Sudoku program. 我正在编写一个简单的Sudoku程序。 Everything works perfectly now except this method: 现在,除以下方法外,一切都可以正常运行:

public static void checkAnswerKey()
    {
        int rowCounter = 0;
        int columnCounter = 0;

        System.out.println("The answers that are correct are (F is incorrect): ");

        for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
        {
            for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
            {
                if (gui.userInputFormattedArray[rowCounter][columnCounter].getText() == io.puzzleAnswerArray[rowCounter][columnCounter].toString())
                {
                    gui.userInputFormattedArray[rowCounter][columnCounter].setBackground(gui.correctAnswer);
                    System.out.print(gui.userInputFormattedArray[rowCounter][columnCounter].getText() + " ");
                }
                else
                {
                    gui.userInputFormattedArray[rowCounter][columnCounter].setBackground(gui.incorrectAnswer);
                    System.out.print(gui.userInputFormattedArray[rowCounter][columnCounter].getText() + io.puzzleAnswerArray[rowCounter][columnCounter].toString() + " ");
                    //System.out.print("F" + " ");
                }
            }
            System.out.println();
        }

This is taken from the game.java file. 这取自game.java文件。

Now, this method is supposed to check the user input against the internal answer. 现在,该方法应该根据内部答案检查用户输入。 Which it does. 是的。 But everything turns up incorrect. 但是一切都变得不正确。

Here are the declarations/instantiations from the io.java and the gui.java files. 这是io.java和gui.java文件中的声明/实例。

public class gui extends JFrame implements ActionListener{

... ...

    public static JFormattedTextField[][] userInputFormattedArray = new JFormattedTextField[9][9];
    //Create the container.
    public Container pane = getContentPane();
    //The font the game uses.
    public Font gameFont = new Font("Arial", Font.PLAIN, 30);
    //Correct and incorrect answer colors.
    static Color correctAnswer = new Color(100, 255, 100);
    static Color incorrectAnswer = new Color(255, 100, 100);

These two methods are launched in the constructor: 这两个方法在构造函数中启动:

public void showTextFields()
    {
        int rowCounter = 0;
        int columnCounter = 0;

        for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
        {
            for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
            {
                userInputFormattedArray[rowCounter][columnCounter] = new JFormattedTextField();
                pane.add(userInputFormattedArray[rowCounter][columnCounter]);
                userInputFormattedArray[rowCounter][columnCounter].setFont(gameFont);
                userInputFormattedArray[rowCounter][columnCounter].setHorizontalAlignment(JTextField.CENTER);
            }
        }
    }

    public void setTextFields()
    {
        String heldString;
        boolean notEditable = false;
        int rowCounter = 0;
        int columnCounter = 0;

        for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
        {
            for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
            {
                if (game.puzzleUserShownArray[rowCounter][columnCounter] != 0)
                {
                    heldString = game.puzzleUserShownArray[rowCounter][columnCounter].toString();
                    userInputFormattedArray[rowCounter][columnCounter].setText(heldString);
                    userInputFormattedArray[rowCounter][columnCounter].setEditable(notEditable);
                }
            }
        }
    }

And this is where the answer comes from: 这是答案的来源:

public static Integer[][] puzzleAnswerArray = new Integer[9][9];

public void getPuzzle() throws FileNotFoundException
    {
        Scanner puzzlesInFile = new Scanner(new FileReader("C:\\Users\\owner\\Desktop\\eclipse\\projects\\Sudoku\\Sudoku\\src\\puzzles.dat"));
        String searchParameter = "#puzzle" + intPuzzleSeed;
        int rowCounter = 0;
        int columnCounter = 0;

        //Prints the search parameter into the console, ensuring accuracy with the RNG.
        System.out.print(searchParameter);
        System.out.println();

        //
        puzzlesInFile.findWithinHorizon(searchParameter, 0);
        while (puzzlesInFile.hasNextInt())
        {
            for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
            {
                for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
                {
                    puzzleAnswerArray[rowCounter][columnCounter] = puzzlesInFile.nextInt();
                    System.out.print(puzzleAnswerArray[rowCounter][columnCounter].toString() + " ");
                }
                System.out.println();
            }
        }
        puzzlesInFile.close();
    }

I hope that this is enough info. 我希望这是足够的信息。 Am I missing something obvious? 我是否缺少明显的东西? Here is the console output from a completed puzzle, by the way: 顺便说一下,这是一个完成的难题的控制台输出:

The answers that are correct are (F is incorrect): 11 22 33 44 55 66 77 88 99 44 55 66 77 88 99 11 22 33 77 88 99 11 22 33 44 55 66 22 33 44 55 66 77 88 99 11 55 66 77 88 99 11 22 33 44 88 99 11 22 33 44 55 66 77 33 44 55 66 77 88 99 11 22 66 77 88 99 11 22 33 44 55 99 11 22 33 44 55 66 77 88 正确的答案是(F不正确):11 22 33 44 55 66 77 88 99 44 55 66 77 88 99 11 22 33 77 88 99 11 22 33 44 55 66 22 33 44 55 66 77 88 99 11 55 66 77 88 99 11 22 33 44 88 99 11 22 33 44 55 66 77 33 44 55 66 77 88 99 11 22 66 77 88 99 11 22 33 44 55 99 11 22 33 44 55 66 77 88

Here is the GUI: 这是GUI:

---EDIT:I can't post the GUI because I have less than 10 reputation. ---编辑:我无法发布GUI,因为我的信誉不到10。 As soon as I get there, I will post a .png of the GUI.--- 到达那里后,我将发布.png的GUI。---

My guess would be that even though the "strings" are the same, it for some reason or another isn't recognizing it as the same. 我的猜测是,即使“字符串”是相同的,由于某种原因还是另一个原因,它也无法将其识别为相同的字符串。 Let me know if you need to see more code, but I think I covered everything. 让我知道是否需要查看更多代码,但我想我已经介绍了所有内容。

You are using == for string equality check. 您正在使用==进行字符串相等性检查。

if(gui.userInputFormattedArray[rowCounter][columnCounter].getText() == io.puzzleAnswerArray[rowCounter][columnCounter].toString()) {

Use .equals() methods instead. 请改用.equals()方法。 And that applies not just for string but for any kind of objects. 这不仅适用于字符串,而且适用于任何类型的对象。 In case of objects == checks the identity of the objects. 如果是对象,则==检查对象的身份。

Try: 尝试:

if(gui.userInputFormattedArray[rowCounter][columnCounter].getText().equals(io.puzzleAnswerArray[rowCounter][columnCounter].toString())) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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