I'm pretty new to java programming yet and I want to try doing a GUI App, and right now I'm kinda stuck on the if/else statement mechanics. So far everything runs except for the "Rock Bet" and "Paper Bet" mechanics. I've tested the Scissor Bet and everything works.
The problem here is that whenever the computer displays its pick and runs through the if/else statement, the verdict is always wrong. How come is the "Rock Bet" and the "Paper Bet" different from the Scissor one?
(I've tried placing the "Scissor Bet" if statement mechanics on the top, before the Rock and Paper if/else statement and somehow the verdict became wrong there. is the arrangement of my order wrong? Please do correct me, Thanks!)
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class gameRPS extends JFrame {
private JPanel mainPanel;
private JLabel textLabel1;
private JButton rockButton;
private JButton paperButton;
private JButton scissorButton;
private JLabel textLabel2;
private JLabel computersLabel;
private JLabel userBet;
private JButton enterButton;
private JLabel verdictLabel;
public gameRPS(String title) {
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(mainPanel);
this.pack();
rockButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
userBet.setText("YOU PICKED: ROCK!");
}
});
paperButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
userBet.setText("YOU PICKED: PAPER!");
}
});
scissorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
userBet.setText("YOU PICKED: SCISSOR!");
}
});
String[] rpsChoices = {"ROCK!", "PAPER!", "SCISSOR!"};
Random rand = new Random();
int computerBet = rand.nextInt(rpsChoices.length);
enterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
String computersChoice = rpsChoices[computerBet];
String userBetRock = "ROCK!";
String userBetPaper = "PAPER!";
String userBetScissor = "SCISSOR!";
computersLabel.setText(computersChoice); //Displays the Computer's Choice
//Mechanics for the Rock Bet
if (userBetRock == "ROCK!" && computersChoice.equals("ROCK!")) {
verdictLabel.setText("DRAW");
}
else if (userBetRock == "ROCK!" && computersChoice.equals("PAPER!")) {
verdictLabel.setText("YOU LOSE");
}
else if (userBetRock == "ROCK!" && computersChoice.equals("SCISSOR!")) {
verdictLabel.setText("YOU WIN");
}
//Mechanics for the Paper Bet
if (userBetPaper == "PAPER!" && computersChoice.equals("PAPER!")) {
verdictLabel.setText("DRAW");
}
else if (userBetPaper == "PAPER!" && computersChoice.equals("ROCK!")) {
verdictLabel.setText("YOU WIN");
}
else if (userBetPaper == "PAPER!" && computersChoice.equals("SCISSOR!")) {
verdictLabel.setText("YOU LOSE");
}
//Mechanics for the Scissor Bet (WORKING)
if (userBetScissor == "SCISSOR!" && computersChoice.equals("SCISSOR!")) {
verdictLabel.setText("DRAW");
}
else if (userBetScissor == "SCISSOR!" && computersChoice.equals("ROCK!")) {
verdictLabel.setText("YOU LOSE");
}
else if (userBetScissor == "SCISSOR!" && computersChoice.equals("PAPER!")) {
verdictLabel.setText("YOU WIN");
}
}
});
}
public static void main(String[] args) {
JFrame frame = new gameRPS("Rock Paper Scissors!");
frame.setSize(300,320);
frame.setResizable(true);
frame.setVisible(true);
}
}
Aucun commentaire:
Enregistrer un commentaire