比较图标作为字符串总是返回false?

发布于 2025-01-31 01:05:01 字数 1189 浏览 3 评论 0原文

我目前正在使用Java Swing进行TIC-TAC-TOE游戏,并弄清楚如何创建Checkwin方法。 TIC-TAC-TOE板被初始化为2D按钮。单击时为每个按钮分配一个图像(交替X和O)。 问题是,即使比较两个具有相同字符串描述的图标,也会返回false。这是我的

  1. 图像分配
    public ImageIcon getImage(){
        BufferedImage img = null;
        String name="";
        try{
            
            if(this.num()== 1){
                img = ImageIO.read(new FileInputStream(new File("x.jpg")));
                name="x";
            }else{
                img = ImageIO.read(new FileInputStream(new File("o.jpg")));
                name="o";
            }
        }catch(Exception e){
            System.out.println(e);
            System.out.println("null :(");
            return null;
        }

        Image scaledImage = img.getScaledInstance(40, 40,Image.SCALE_SMOOTH);
        ImageIcon imageIcon = new ImageIcon(scaledImage,name);
        return imageIcon;
    }

的代码,这是平等比较的代码段(我不知道为什么,但是我的2D数组打印出列主要顺序而不是行主要顺序)

buttons[i][j].getIcon().equals(buttons[i-1][j].getIcon()));

比较下面的两个o返回false false

这是我第一次发布溢出,请耐心等待我:)

I'm currently working on a Tic-Tac-Toe game with java swing and figuring out how to create a checkWin method. The Tic-Tac-Toe board is initialized as a 2D array of buttons. Each button is assigned an image when clicked (alternating x's and o's).
The problem is, even when comparing two icons with the same string description, it returns false. Here's my code for

  1. The Image assignment
    public ImageIcon getImage(){
        BufferedImage img = null;
        String name="";
        try{
            
            if(this.num()== 1){
                img = ImageIO.read(new FileInputStream(new File("x.jpg")));
                name="x";
            }else{
                img = ImageIO.read(new FileInputStream(new File("o.jpg")));
                name="o";
            }
        }catch(Exception e){
            System.out.println(e);
            System.out.println("null :(");
            return null;
        }

        Image scaledImage = img.getScaledInstance(40, 40,Image.SCALE_SMOOTH);
        ImageIcon imageIcon = new ImageIcon(scaledImage,name);
        return imageIcon;
    }

Here's the code snippet for equality comparison (also I have no idea why, but my 2d array prints out column major order rather than row major order)

buttons[i][j].getIcon().equals(buttons[i-1][j].getIcon()));

comparing the two o's below returns false

This is my first time posting on overflow please be patient with me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

优雅的叶子 2025-02-07 01:05:01

您应该使用整数代表“ X和'O”(例如1和0)。然后,将它们显示为屏幕上的图像?

You should use integers instead, to represent 'x's and 'o's(like 1 and 0). And then, show them as images on the screen?

醉梦枕江山 2025-02-07 01:05:01

最好使用枚举来比较,而不是像其他人所建议的那样的图标或整数。为什么不整数?因为它们不像枚举中使用常数那样可读 - 您必须记住1或0的含义。还有其他理由也喜欢枚举:

  • 整数字段允许任何适合的值,但只有一组有限的可能性。这对读者来说是误导的。字符串会有相同的问题。
  • 由于枚举是一组有限的可能性,因此IDE可以在您想比较或以其他方式使用这些值时自动建议它们。

It'd be better to use an enumeration to compare, rather than the icons or integers as others have suggested. Why not integers? Because they're not as readable as using constants from an enumeration - you have to remember what 1 or 0 mean. There are other reasons to prefer an enumeration, too:

  • An integer field allows any value that fits, but there are only a limited set of possibilities. It's misleading for a reader. Strings would have the same problem.
  • Since the enumeration is a limited set of possibilities, an IDE can automatically suggest them all when you do want to compare, or otherwise use the values.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文