Ask your own question, for FREE!
Computer Science 23 Online
OpenStudy (anonymous):

How can I make this if statement work? I have three images that auto generate and if all 3 are the same I want message "You won!" to pop up. @Override public void actionPerformed(ActionEvent e) { //PlaySound.play("C:/Users/wliu.GGC/Music/playSound.wav"); Random r = new Random(); ImageIcon picture1 = new ImageIcon("/Users/lalo/Desktop/pic/" + r.nextInt(3) + ".jpg"); ImageIcon picture2 = new ImageIcon("/Users/lalo/Desktop/pic/" + r.nextInt(3) + ".jpg"); ImageIcon picture3 = new ImageIcon("/Users/lalo/Desktop/pic/" + r.nextInt(3) + ".jpg"); label1.setIcon(picture1); label2.setIcon(picture2); label3.setIcon(picture3); if (label1==label2 && label2==label3){ JOptionPane.showMessageDialog(null, "You won!"); } }

OpenStudy (anonymous):

i am not sure you can compare it like that because label1 equals picture1 not picture 2. what about having a condition to determine which picture the label gets? if (this) label2.setIcon(picture1) else if (this) label2.setIcon(picture2) then perhaps you could compare in this way?

OpenStudy (anonymous):

and if not, you could use variables in a pinch. if (this){ label2.setIcon(picture1); intLabel2=1; } then if (intLabel2==intLabel3 && intLabel2 == intLabel1) "youwin!" might not be the most accurate way to do it, but then again, I tend to just force code to work when i don't know the shortcut way!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!