Question:
Java help please ROCK PAPER SCISSORS game?
2011-12-19 19:15:46 UTC
I am trying to create this program of a basic game called rock paper scissors. The game is between you and the computer. It is suppose to display who wins in each round and it has the round number. Your suppose to continue playing until either you or the computer wins more than two times. Then It's suppose to display who the final winner is and it's suppose to display the number of rounds that were played. But, for some reason when the game is complete it doesn't display the correct number of rounds played. Also, the game plays way more then the two times it should be playing. Could someone please help thanks. Here's my code.

import javax.swing.JOptionPane;


class ScissorsRockPaper
{
public static void main(String[] args)
{

int randomNumber=0;
int humanChoice=0;
String humanQuestion;
int human_wins=1;
int comp_wins=1;


JOptionPane.showMessageDialog(null, "Rock - Paper - Scissors\nWhoever wins more than two times is the winner!");

do {
humanQuestion=JOptionPane.showInputDialog(null, "Computer's Move: x\nYour Move:", "Scissor - Rock - Paper", JOptionPane.QUESTION_MESSAGE);
humanChoice=Integer.parseInt(humanQuestion);
randomNumber = (int)(Math.random() * 3);


if (humanChoice==0 && randomNumber==0)
{
JOptionPane.showMessageDialog(null, "Computer is Scissors\nYours is Scissors\nIt is a Tie");
}
else if (humanChoice==1 && randomNumber==1)
{
JOptionPane.showMessageDialog(null, "Computer is Rock\nYours is Rock\nIt is a Tie");
}
else if (humanChoice==2 && randomNumber==2)
{
JOptionPane.showMessageDialog(null, "Computer is Paper\nYours is Paper\nIt is a Tie");
}

else if (humanChoice==1 && randomNumber==2)
{
JOptionPane.showMessageDialog(null, "Computer is Paper\nYours is Rock\nComputer Win and You Lose in round #" + comp_wins);
comp_wins++;
}
else if (humanChoice==2 && randomNumber==0)
{
JOptionPane.showMessageDialog(null, "Computer is Scissors\nYours is Paper\nComputer Win and You Lose in round #" + comp_wins);
comp_wins++;
}
else if (humanChoice==0 && randomNumber==1)
{
JOptionPane.showMessageDialog(null, "Computer is Rock\nYours is Scissors\nComputer Win and You Lose in round #" + comp_wins);
comp_wins++;
}
else if (humanChoice==2 && randomNumber==1)
{
JOptionPane.showMessageDialog(null, "Computer is Rock\nYours is paper\nYou Win and Computer Lose in round #" + human_wins);
human_wins++;
}
else if (humanChoice==0 && randomNumber==2)
{
JOptionPane.showMessageDialog(null, "Computer is Paper\nYours is Scissors\nYou Win and Computer Lose in round #" + human_wins);
human_wins++;
}
else if (humanChoice==1 && randomNumber==0)
{
JOptionPane.showMessageDialog(null, "Computer is Scissors\nYours is Rock\nYou Win and Computer Lose in round #" + human_wins);
human_wins++;
}
else
{
JOptionPane.showMessageDialog(null, "Sorry! No such choice. Please enter (0) for Scissors, (1) for Rock, and (2) for Paper");
}
} while (human_wins<=2 || comp_wins<=2); // end do while loop



/* after the total rounds are over
print out the winner of the game*/

if (humanChoice>randomNumber)
{
JOptionPane.showMessageDialog(null, "Congratulations! You are the final winner after the total of " + human_wins + " rounds");
}
else if (randomNumber>humanChoice)
{
JOptionPane.showMessageDialog(null, "Bad Luck! Computer is the final winner after the total of " + comp_wins + " rounds");
}

} // End Main
} // End Class
Four answers:
b4iquit
2011-12-19 21:55:22 UTC
OK, I have made a number of changes, added some additional variables and exception handling when the user fail to input an int, although I don't test to be sure its 0, 1 or 2. First problem was your OR test in you while. It needs to be AND, so if one gets 3 wins the loop breaks.

I am keeping track of ROUNDS and TIES so you can see.

The result at the end is not dependent on the last human v computer choice, but on who has the most wins.

When wins are printed out at the end, so are the number of rounds (which will be greater than the number of wins (human and computer) because of Ties) Ties are also printed out.

Hope this helps.





Be safe, be sage







import javax.swing.JOptionPane;





class ScissorsRockPaper

{

public static void main(String[] args)

{



int randomNumber=0;

int humanChoice=0;

String humanQuestion;

int human_wins=0;

int comp_wins=0;

int rounds = 0 ;

int ties = 0 ;





JOptionPane.showMessageDialog(null, "Rock - Paper - Scissors\nWhoever wins more than two times is the winner!");



do {



try {

humanQuestion=JOptionPane.showInputDialog(null, "Computer's Move: x\nYour Move:", "Scissor - Rock - Paper : Round " + (rounds+1) , JOptionPane.QUESTION_MESSAGE);

humanChoice=Integer.parseInt(humanQuestion) ;

randomNumber = (int)(Math.random() * 3);

rounds++ ;



if (humanChoice==0 && randomNumber==0)

{

JOptionPane.showMessageDialog(null, "Computer is Scissors\nYours is Scissors\nIt is a Tie");

ties++ ;

}

else if (humanChoice==1 && randomNumber==1)

{

JOptionPane.showMessageDialog(null, "Computer is Rock\nYours is Rock\nIt is a Tie");

ties++ ;

}

else if (humanChoice==2 && randomNumber==2)

{

JOptionPane.showMessageDialog(null, "Computer is Paper\nYours is Paper\nIt is a Tie");

ties++ ;

}



else if (humanChoice==1 && randomNumber==2)

{

JOptionPane.showMessageDialog(null, "Computer is Paper\nYours is Rock\nComputer Win and You Lose in round #" + rounds);

comp_wins++;

}

else if (humanChoice==2 && randomNumber==0)

{

JOptionPane.showMessageDialog(null, "Computer is Scissors\nYours is Paper\nComputer Win and You Lose in round #" + rounds);

comp_wins++;

}

else if (humanChoice==0 && randomNumber==1)

{

JOptionPane.showMessageDialog(null, "Computer is Rock\nYours is Scissors\nComputer Win and You Lose in round #" + rounds);

comp_wins++;

}

else if (humanChoice==2 && randomNumber==1)

{

JOptionPane.showMessageDialog(null, "Computer is Rock\nYours is paper\nYou Win and Computer Lose in round #" + rounds);

human_wins++;

}

else if (humanChoice==0 && randomNumber==2)

{

JOptionPane.showMessageDialog(null, "Computer is Paper\nYours is Scissors\nYou Win and Computer Lose in round #" + rounds);

human_wins++;

}

else if (humanChoice==1 && randomNumber==0)

{

JOptionPane.showMessageDialog(null, "Computer is Scissors\nYours is Rock\nYou Win and Computer Lose in round #" + rounds);

human_wins++;

}

else

{

JOptionPane.showMessageDialog(null, "Sorry! No such choice. Please enter (0) for Scissors, (1) for Rock, and (2) for Paper");

}



} catch (Exception e ) {

JOptionPane.showMessageDialog(null, "Rock - Paper - Scissors\nYou must pick 0, 1 or 2" ) ;

}



} while (human_wins<=2 && comp_wins<=2); // end do while loop







/* after the total rounds are over

print out the winner of the game*/



if (human_wins>comp_wins)

{

JOptionPane.showMessageDialog(null, "Congratulations! You are the final winner after the total of " + human_wins + " wins out of " + rounds + " rounds. Computer wins " + comp_wins + " Ties " + ties );

}

else if (comp_wins>human_wins)

{

JOptionPane.showMessageDialog(null, "Bad Luck! Computer is the final winner after the total of " + comp_wins + " wins out of " + rounds + " rounds. Human wins " + human_wins + " Ties " + ties );

}



} // End Main

} // End Class
?
2016-10-02 11:35:10 UTC
Rock Paper Scissors Java
2011-12-19 19:16:54 UTC
Search it in the internet
?
2011-12-19 19:16:54 UTC
Use rock.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...