Reputation: 21
I'm trying to solve this assignment for our subject. I've already tried it without using the JOptionPane
and it worked, but when I edited it to have a dialog box, it didn't worked. The first dialog box should contain a title and the choices to be inputted, the last one should enable the user to quit the program. After the user enter his/her choice, a dialog box should appear indicating the user's choice and the computer's choice. The computer's choice should be random. If ever the user didn't input a correct option, a dialog box would appear telling the user to input a valid option. This is what I have so far:
package assignment;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
public class RockPaperScissorGame
{
public static void main(String[] args)
{
String inputStr;
String personPlay="";
String computerPlay="1,2,3";
int computerInt;
Scanner input = new Scanner(System.in);
Random generator = new Random();
inputStr = JOptionPane.showInputDialog("Lets play a game! \nEnter 1 for rock \nEnter 2 for paper \nEnter 3 for scissors \nEnter 4 to quit");
personPlay = input.next(inputStr);
switch (computerInt = 0)
{
}
do
{
if (personPlay.equals(computerPlay))
JOptionPane.showMessageDialog(null, "It's a TIE! ", "TIE!", JOptionPane.INFORMATION_MESSAGE);
else if (personPlay.equals("1"))
{
if (computerPlay.equals("3"))
JOptionPane.showMessageDialog(null, "Rock beats Scissors. \nYOU WIN! ", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Paper beats Rock. \nYOU LOSE! ", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE);
}
else if (personPlay.equals("2"))
{
if (computerPlay.equals("3"))
JOptionPane.showMessageDialog(null, "Scissor beats Paper. \nYOU LOSE!", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Paper beats Rock. \nYOU WIN! ", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE);
}
else if (personPlay.equals("3"))
{
if (computerPlay.equals("1"))
JOptionPane.showMessageDialog(null, "Scissor beats Paper. \nYOU WIN!", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Rock beats Scissors. \nYOU LOSE! ", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE);
}
else if (personPlay.equals("4"))
{
JOptionPane.showMessageDialog(null, "GOOD BYE", " BYE!", JOptionPane.INFORMATION_MESSAGE);
}
}while(true);
}
}
Hope you guys can help. Thank you :)
Upvotes: 2
Views: 3658
Reputation: 41152
computerPlay
, which is supposed to be randomly chosen.showInputDialog
should be inside the loop, otherwise the game never ends...switch
is useless.Scanner
. Just do: personPlay = JOptionPane.showInputDialog("Lets play a game! [...]");
Upvotes: 2