I have been working on a small Rock Paper Scissors game with Java, where the win or lose condition is based on whether the computer or the player wins five games. I am not sure how to get the program to loop at the user input should there be a wrong input.
Here is the code where I'm having this trouble
The section that I'm trying to loop is the portion with "else if (determination.equals("Y")) {"
import java.util.*;
public class Rock_Paper_Scissors {
public static void main(String arg[]) {
boolean loopGameStart = true;
while (loopGameStart) {
System.out.println("Welcome to Rock Paper Scissors, a game programmed "
+ "by Daniel Park. Would you like to start? (Y/N)");
Scanner userInput = new Scanner(System.in);
String determination = userInput.next();
if (determination.equals("N")) {
System.out.println("Please, do reconsider...");
loopGameStart = true;
} else if (determination.equals("Y")) {
Random rand = new Random();
int n = rand.nextInt(3) + 1;
// 1 = Rock, 2 = Paper, 3= Scissor
int humanWinCount = humanWinCount();
int computerWinCount = computerWinCount();
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
Scanner userRPS = new Scanner(System.in);
int choiceRPS = userRPS.nextInt();
while ((humanWinCount < 5) && (computerWinCount < 5)) {
if (choiceRPS == 0) {
if (n == 1) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try Again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
} else if (choiceRPS == 1) {
if (n == 1) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
} else if (choiceRPS == 2) {
if (n == 1) {
System.out.println("LOSS");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
}
}
if (humanWinCount == 5) {
System.out.println("Congratulations, you win!!");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
}
if (computerWinCount == 5) {
System.out.println("Boohoo, you lost!!");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
}
} else {
System.out.println("I do not understand, please try again!");
}
}
}
public static int humanWinCount() {
int x = 0;
return x;
}
public static int computerWinCount() {
int c = 0;
return c;
}
}
Please, do not write everything in one main function, or repeat the same code everywhere, and etc.
I completely rebuild your code and also implemented the loop #Austin suggested. Now your should be working as you which or very close. Enjoy:
import java.util.*;
public class Rock_Paper_Scissors
{
public static void main( String arg[] )
{
Rock_Paper_Scissors game = new Rock_Paper_Scissors();
game.startLogic();
}
private Random rand;
private Scanner inputScanner;
private boolean loopGameStart;
private int humanWinCount;
private int computerWinCount;
public Rock_Paper_Scissors()
{
this.rand = new Random();
this.inputScanner = new Scanner( System.in );
this.loopGameStart = true;
humanWinCount = 0;
computerWinCount = 0;
}
public void startLogic()
{
System.out.print( "Welcome to Rock Paper Scissors, a game programmed by Daniel Park. " );
this.askUntilGetAnAnswer( "N" );
}
private int humanWinCount()
{
int x = 0;
return x;
}
private int computerWinCount()
{
int c = 0;
return c;
}
private void askUntilGetAnAnswer( String determination )
{
while( this.loopGameStart )
{
if( !determination.equals( "Y" ) )
{
System.out.println( "\nWould you like to start? (Y/N)" );
determination = this.inputScanner.next();
}
if( determination.equals( "N" ) )
{
System.out.println( "Please, do reconsider..." );
this.loopGameStart = false;
}
else if( determination.equals( "Y" ) )
{
this.processCoreGameLogic();
}
else
{
System.out.print( "I do not understand, please try again!" );
}
}
}
private void processCoreGameLogic()
{
int choiceRPS;
System.out.println( "Choose 0, 1, or 2 (Rock/Paper/Scissor)" );
// 1 = Rock, 2 = Paper, 3= Scissor
this.humanWinCount = this.humanWinCount();
this.computerWinCount = this.computerWinCount();
while( ( this.humanWinCount < 5 ) && ( this.computerWinCount < 5 ) )
{
try
{
choiceRPS = inputScanner.nextInt();
if( choiceRPS >= 0 && choiceRPS <= 2 )
{
this.playing( choiceRPS );
}
}
catch( InputMismatchException e )
{
System.out.print( "I do not understand, please try again!" );
inputScanner.next();
}
}
this.endGame();
}
private void endGame()
{
String contOrNot = "";
if( this.humanWinCount == 5 )
{
System.out.println( "\nCongratulations, you win!!" );
System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
System.out.println( "You: " + this.humanWinCount + " rounds won" );
System.out.println( "Would you like to try again? (Y/N)" );
contOrNot = this.inputScanner.next();
}
if( this.computerWinCount == 5 )
{
System.out.println( "\nBoohoo, you lost!!" );
System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
System.out.println( "You: " + this.humanWinCount + " rounds won" );
System.out.println( "Would you like to try again? (Y/N)" );
contOrNot = this.inputScanner.next();
}
if( contOrNot.equals( "N" ) )
{
System.out.println( "Okay, goodbye!!" );
this.loopGameStart = false;
}
else
{
askUntilGetAnAnswer( contOrNot );
}
}
private void playing( int choiceRPS )
{
int randomInteger = this.rand.nextInt( 3 ) + 1;
// choiceRPS 0 -> tie 1, loss 2, win 3
// choiceRPS 1 -> win 3, tie 1, loss 2
// choiceRPS 2 -> loss 2, win 3, tie 1
//
switch( choiceRPS*3 + randomInteger )
{
case 1:
case 5:
case 9:
{
show_tie();
break;
}
case 2:
case 6:
case 7:
{
show_loss();
break;
}
case 3:
case 4:
case 8:
{
show_win();
break;
}
default:
{
System.out.println( " I do not understand... Try again." );
System.out.println( " Choose 0, 1, or 2 again (Rock/Paper/Scissor)" );
System.out.println( " Computer: " + this.computerWinCount + " rounds won" );
System.out.println( " You: " + this.humanWinCount + " rounds won" );
}
}
}
private void show_loss()
{
System.out.println( "LOSS" );
System.out.println( "Choose 0, 1, or 2 again (Rock/Paper/Scissor)" );
this.computerWinCount = this.computerWinCount + 1;
System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
System.out.println( "You: " + this.humanWinCount + " rounds won" );
}
private void show_win()
{
System.out.println( "WIN!!" );
System.out.println( "Choose 0, 1, or 2 again (Rock/Paper/Scissor)" );
this.humanWinCount = this.humanWinCount + 1;
System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
System.out.println( "You: " + this.humanWinCount + " rounds won" );
}
private void show_tie()
{
System.out.println( "TIE!!" );
System.out.println( "Choose 0, 1, or 2 again (Rock/Paper/Scissor)" );
System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
System.out.println( "You: " + this.humanWinCount + " rounds won" );
}
}
You can use a do while
int choiceRPS;
do{
choiceRPS = userRPS.nextInt();
}while(choiceRPS<0||choiceRPS>2);
Your logic looks very complex. It can be easily simplified as the game has only 3 outcomes other that a tie.
SCISSORS beats PAPER
PAPER beats ROCK
ROCK beats SCISSORS
So considering the inputs as 0, 1 and 2 representing Rock, Paper and Scissors respectively:
You need to keep track of two numbers. One is user entered choice
let's call it userChoice and another being computerChoice which
is computer's played choice. This computerChoice should be a
randomly chosen digit between 0 and 2 both inclusive.
Now, start comparing both the numbers to decide on the winner and
increment the respective counter (humanWinCount and
computerWinCount).
Stop the game and decide the winner when either the user or the
computer wins at least 5 rounds.
Hence, your gaming loop should be looking something like this:
while(true){
System.out.println();
/*
* If either one wins, break out of the game
* and decide the winner.
*/
if(humanWinCount == 5 || computerWinCount == 5){
break;
}
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
//Read user's choice
int userChoice = Integer.parseInt(sc.nextLine());
//Computer will play it's turn
int computerChoice = rand.nextInt(3);
//Start comparing player's choice vs Computer's choice
if(userChoice == computerChoice){
tieCount++;
showTieMessage();
}else if(userChoice == 0 && computerChoice == 1){
//User plays ROCK and computer plays PAPER
computerWinCount++;
showComputerWinMessage();
}else if(userChoice == 0 && computerChoice == 2){
//User plays ROCK and computer plays SCISSORS
humanWinCount++;
showHumanWinMessage();
}else if(userChoice == 1 && computerChoice == 0){
//User plays PAPER and computer plays ROCK
humanWinCount++;
showHumanWinMessage();
}else if(userChoice == 1 && computerChoice == 2){
//User plays PAPER and computer plays SCISSORS
computerWinCount++;
showComputerWinMessage();
}else if(userChoice == 2 && computerChoice == 0){
//User plays SCISSORS and computer plays ROCK
computerWinCount++;
showComputerWinMessage();
}else if(userChoice == 2 && computerChoice == 1){
//User plays SCISSORS and computer plays PAPER
humanWinCount++;
showHumanWinMessage();
}else{
System.out.println("Unrecougnized user input!!");
System.out.println("Please Enter correct values!!");
continue;
}
}
And the entire remodeled code should be looking something like this:
public class RockPepperScissor {
private Random rand;
private int humanWinCount;
private int computerWinCount;
private int tieCount; //Not Required, but still keeping in track.
public RockPepperScissor(){
rand = new Random();
humanWinCount = 0;
computerWinCount = 0;
tieCount = 0;
}
public void startGame(){
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Rock Paper Scissors, a game programmed by Daniel Park. Would you like to start? (Y/N)");
String userChoice = sc.nextLine();
if(userChoice.equalsIgnoreCase("Y")){
startGameLogic(sc);
}else{
if(userChoice.equalsIgnoreCase("N")){
System.out.println("Do you want to reconsider?? (Y/N)");
String secondChance = sc.nextLine();
if(secondChance.equalsIgnoreCase("Y")){
startGameLogic(sc);
}else{
System.exit(0);
}
}else{
System.out.println("Unable to identify your input!! Stopping the game!!");
System.exit(0);
}
}
sc.close();
}
public void startGameLogic(Scanner sc){
while(true){
System.out.println();
/*
* If either one wins, break out of the game
* and decide the winner.
*/
if(humanWinCount == 5 || computerWinCount == 5){
break;
}
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
//Read user's choice
int userChoice = Integer.parseInt(sc.nextLine());
//Computer will play it's turn
int computerChoice = rand.nextInt(3);
//Start comparing player's choice vs Computer's choice
if(userChoice == computerChoice){
tieCount++;
showTieMessage();
}else if(userChoice == 0 && computerChoice == 1){
//User plays ROCK and computer plays PAPER
computerWinCount++;
showComputerWinMessage();
}else if(userChoice == 0 && computerChoice == 2){
//User plays ROCK and computer plays SCISSORS
humanWinCount++;
showHumanWinMessage();
}else if(userChoice == 1 && computerChoice == 0){
//User plays PAPER and computer plays ROCK
humanWinCount++;
showHumanWinMessage();
}else if(userChoice == 1 && computerChoice == 2){
//User plays PAPER and computer plays SCISSORS
computerWinCount++;
showComputerWinMessage();
}else if(userChoice == 2 && computerChoice == 0){
//User plays SCISSORS and computer plays ROCK
computerWinCount++;
showComputerWinMessage();
}else if(userChoice == 2 && computerChoice == 1){
//User plays SCISSORS and computer plays PAPER
humanWinCount++;
showHumanWinMessage();
}else{
System.out.println("Unrecougnized user input!!");
System.out.println("Please Enter correct values!!");
continue;
}
}
}
public void showHumanWinMessage(){
System.out.println("YOU WON!!");
System.out.println("Your wins: " + humanWinCount);
System.out.println("Computer wins: " + computerWinCount);
System.out.println("Ties: " + tieCount);
}
public void showComputerWinMessage(){
System.out.println("YOU LOST!!");
System.out.println("Your wins: " + humanWinCount);
System.out.println("Computer wins: " + computerWinCount);
System.out.println("Ties: " + tieCount);
}
public void showTieMessage(){
System.out.println("Tie!!");
System.out.println("Your wins: " + humanWinCount);
System.out.println("Computer wins: " + computerWinCount);
System.out.println("Ties: " + tieCount);
}
public void decideWinner(){
if(humanWinCount > computerWinCount){
System.out.println();
System.out.println("Yippee!! You won the Game!!");
}else if(computerWinCount > humanWinCount){
System.out.println();
System.out.println("Oops!! You lost the Game!!");
System.out.println("Better Luck Next Time!!");
}
}
public static void main(String[] args) {
RockPepperScissor rps = new RockPepperScissor();
rps.startGame();
rps.decideWinner();
}
}
I hope this helps you in your learning activity.
So it's pretty much visible that the code is not modular and there are redundant statements in the code. They might still be there, since all I did was to fix the logic and make comments on the code for the changes introduced -
public class Rock_Paper_Scissors {
public static void main(String arg[]) {
boolean loopGameStart = true;
while (loopGameStart) {
System.out.println(
"Welcome to Rock Paper Scissors, a game programmed by Daniel Park. Would you like to start? (Y/N)");
Scanner userInput = new Scanner(System.in);
String determination = userInput.next();
switch (determination) {
case "N":
System.out.println("Please, do reconsider...");
loopGameStart = true; // you might not want the player to be trapped here, so change it to `false` rather
break;
case "Y":
/** 0 = Rock, 1 = Paper, 2 = Scissor */
int humanWinCount = 0;
int computerWinCount = 0;
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
Scanner userRPS = new Scanner(System.in);
int userChoice = userRPS.nextInt();
while ((humanWinCount < 5) && (computerWinCount < 5)) { // either of them reaches 5
int computerChoice = new Random().nextInt(3); // this is fun, no more static computer choice
if (userChoice == computerChoice) { // break quick if its a tie
System.out.println("TIE!!");
displayStats(computerWinCount, humanWinCount);
askChoice();
} else {
switch (userChoice) { //based on userChoice and computerChoice combination
case 0:
if (computerChoice == 1) {
System.out.println("LOSS!!");
displayStats(computerWinCount++,
humanWinCount); // incrementing the computerCount as well
askChoice();
} else if (computerChoice == 2) {
System.out.println("WIN!!");
displayStats(computerWinCount,
humanWinCount++); // incrementing the humanCount as well
askChoice();
}
break;
case 1:
if (computerChoice == 0) {
System.out.println("WIN!!");
displayStats(computerWinCount, humanWinCount++);
askChoice();
} else if (computerChoice == 2) {
System.out.println("LOSS!!");
displayStats(computerWinCount++, humanWinCount);
askChoice();
}
break;
case 2:
if (computerChoice == 0) {
System.out.println("LOSS");
displayStats(computerWinCount++, humanWinCount);
askChoice();
} else if (computerChoice == 1) {
System.out.println("WIN!!");
displayStats(computerWinCount, humanWinCount++);
askChoice();
}
break;
default:
System.out.println("I do not understand... Try again.");
displayStats(computerWinCount, humanWinCount);
askChoice();
break;
}
}
userChoice = userRPS.nextInt(); //next iteration input only required once
}
if (humanWinCount == 5) {
System.out.println("Congratulations, you win!!");
} else {
System.out.println("Boohoo, you lost!!");
}
displayStats(computerWinCount, humanWinCount);
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
break;
default:
System.out.println("I do not understand, please try again!");
break;
}
}
}
private static void askChoice() {
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
}
private static void displayStats(int computerWinCount, int humanWinCount) {
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
}
}
The code explanation would be something that I would leave on what you won't understand in it. So please ask further for anything that you might want to know about why it's being used.
If you need to loop certain code with conditional behaviour, try to use recursive parametric method and enclapsure the conditional parts code each into separate method. These methods will be called from the current recursion. Then call first-time recursion level from main method.
Pseudo-sample:
public int myRecursiveMethod_RPS (int aVariable)
{
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
...
// your own logic below - this is just sample
if (aVariable == 0)
return myRecursiveMethod_RPS(0);
else if (aVariable >= 0)
return myRecursiveMethod_RPS(aVariable);
else return aVariable;
}
Good article about recursion is here Recursion - Java or
here https://howtoprogramwithjava.com/java-recursion/
Try using a do/while loop. The code is run until a certain condition is no longer met, but at least it will be run once even if the condition is not true at the beginning. Using your first input as an example, it would look just like this:
String determination; // I move the variable determination outside so it is in the scope of the while.
do {
System.out.println(
"Welcome to Rock Paper Scissors, a game programmed by Daniel Park. Would you like to start? (Y/N)");
Scanner userInput = new Scanner(System.in);
determination = userInput.next();
} while (determination != "Y" || determination != "N");
//...
The code will at least be run once. If at anytime 'determination' equals "N" or "Y", it will go on with the rest of the code.
You can get more information from this website:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
As i already edited into addons_zz modular version of your program Perambulation: The Scanner that is used for userinput throws an InputMismatchException if you input e.g. "a" and the nextInt() Method of the Scanner is called. The Input "a" can't be cast to the type integer since its a character of the alphabet. A solution to this could look like(in your version of the code):
...
} else if (determination.equals("Y")) {
Random rand = new Random();
int n = rand.nextInt(3) + 1;
// 1 = Rock, 2 = Paper, 3= Scissor
int humanWinCount = humanWinCount();
int computerWinCount = computerWinCount();
Scanner userRPS = new Scanner(System.in);
// loop until choiceRPS is 0, 1 or 2
int choiceRPS = 9;
boolean choiceRPSNotSet = true;
while(choiceRPSNotSet){
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
try
{
choiceRPS = userRPS.nextInt();
if( choiceRPS >= 0 && choiceRPS <= 2 )
{
choiceRPSNotSet = false;
}
}
catch( InputMismatchException e )
{
System.out.println( "I do not understand, please try again!" );
userRPS.next();
}
}
// loop until choiceRPS is 0, 1 or 2
while ((humanWinCount < 5) && (computerWinCount < 5)) {
...
So i added another while loop based on the condition that choiceRPS is set correctly.
if( choiceRPS >= 0 && choiceRPS <= 2 )
{
choiceRPSNotSet = false;
}
Only then the loop will be left which includes catching Exceptions since this part is located in the try/catch block.
Calling the userRPS.next() (or Scanner.next()) Method if an InputMismatchException occurs will clean the Input from the Scanner so that a new input can be read.
An alternative which i would recommend would be encapsulating the userinput into its own method like:
public static int choose(){
Scanner userRPS = new Scanner(System.in);
// loop until choiceRPS is 0, 1 or 2
int choiceRPS = 9;
boolean choiceRPSNotSet = true;
while(choiceRPSNotSet){
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
try
{
choiceRPS = userRPS.nextInt();
if( choiceRPS >= 0 && choiceRPS <= 2 )
{
choiceRPSNotSet = false;
}
}
catch( InputMismatchException e )
{
System.out.println( "I do not understand, please try again!" );
userRPS.next();
}
}
// loop until choiceRPS is 0, 1 or 2
return choiceRPS;
}
And calling this choose() method whenever necessary like:
...
} else if (determination.equals("Y")) {
Random rand = new Random();
int n = rand.nextInt(3) + 1;
// 1 = Rock, 2 = Paper, 3= Scissor
int humanWinCount = humanWinCount();
int computerWinCount = computerWinCount();
int choiceRPS = choose();
while ((humanWinCount < 5) && (computerWinCount < 5)) {
if (choiceRPS == 0) {
if (n == 1) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = choose();
} else if (n == 2) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = choose();
...
Hope this helps! ^^
import java.util.*;
public class Rock_Paper_Scissors {
public static void main(String arg[]) {
boolean loopGameStart = true;
while (loopGameStart) {
System.out.println("Welcome to Rock Paper Scissors, a game programmed "
+ "by Daniel Park. Would you like to start? (Y/N)");
Scanner userInput = new Scanner(System.in);
String determination = userInput.next();
//1=THIS CHANGED TO IGNORE CASE N or n Y or y accepted
if (determination.equalsIgnoreCase("N")) {
System.out.println("Please, do reconsider...");
loopGameStart = true;
} else if (determination.equals("Y")) {
Random rand = new Random();
int n = rand.nextInt(3) + 1;
// 1 = Rock, 2 = Paper, 3= Scissor
int humanWinCount = humanWinCount();
int computerWinCount = computerWinCount();
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
Scanner userRPS = new Scanner(System.in);
int choiceRPS = userRPS.nextInt();
while ((humanWinCount < 5) && (computerWinCount < 5)) {
if (choiceRPS == 0) {
if (n == 1) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try Again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
} else if (choiceRPS == 1) {
if (n == 1) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
} else if (choiceRPS == 2) {
if (n == 1) {
System.out.println("LOSS");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
}else{
//###################################THIS IS THE ADDED CODE
System.out.println("INVALID VALUE ENTER AGAIN");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
}
if (humanWinCount == 5) {
System.out.println("Congratulations, you win!!");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
}
if (computerWinCount == 5) {
System.out.println("Boohoo, you lost!!");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
}
} else {
System.out.println("I do not understand, please try again!");
}
}
}
public static int humanWinCount() {
int x = 0;
return x;
}
public static int computerWinCount() {
int c = 0;
return c;
}
}
Just a simple loop would do the trick :
Scanner answerScanner = new Scanner(System.in);
do{
char answer = answerScanner.next().charAt(0);
}while(answer != 'Y' && answer != 'N');
// Then do whatever you need to do
if(answer == 'Y'){
// Something
}else{
// Something else
}
Note that you could use answerScanner.next().toUpperCase().charAt(0) if you wanted to allow 'y' and 'n' as answers.
This question already has answers here:
Why do we need break after case statements?
(17 answers)
Closed 7 years ago.
first time poster, and newbie coder. I apologize for the mess of this code, still needs cleaning. However, I am having major issues with my switch. If I select case 1, it runs through case 1, then after completion, automatically runs through case 2, then case 3. Likewise, if I select case 2, after completion of case 2, it runs through case 3. Finally, my default isn't working either. When selecting anything other than 1, 2, or 3, all sorts of errors appear. Any help would be greatly appreciated!
Had a hard time searching for this complex issue!
switch(input3)
{
case 1 :
{
JOptionPane.showMessageDialog(null,"You have selected the Rock, Paper, Scissors Game\nThe program is now loading...\nProgram loaded successfully! Please press OK.");
JOptionPane.showMessageDialog(null,"Rock, Paper, Scissors Game... 3 Rounds!");
while(round<3) {
String UserInput = JOptionPane.showInputDialog("Rock, Paper, Scissors Game: Type Rock, Paper, or Scissors");
Random Game = new Random();
int Computer = 1+Game.nextInt(3);
int Scissors, Rock, Paper;
Rock = 1;
Paper = 2;
Scissors= 3;
if(UserInput.equals("Rock")) {
Player = 1;
}
if(UserInput.equals("Paper")) {
Player = 2;
}
if(UserInput.equals("Scissors")) {
Player = 3;
}
int random = (int)(Math.random() * 3);
String computerInput; // Computer's choice
if (random == 0)
computerInput = "rock";
else if (random == 1)
computerInput = "paper";
else
computerInput = "scissor";
while (Player > 3 || Player < 1) {
losses++;
round++;
System.out.println("Not a valid input! Computer wins");
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish tie scenarios using if statements
if(UserInput== computerInput){
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
round++;
}
//Winning scenerios
if(Player == Scissors)
if(Computer == Paper){
JOptionPane.showMessageDialog(null,"Scissors v Paper! Player Wins!");
wins++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if(Computer == Rock){
JOptionPane.showMessageDialog(null,"Scissors v Rock! Computer Wins!");
losses++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player wins
if(Player == Rock)
if(Computer == Scissors ){
JOptionPane.showMessageDialog(null,"Rock v Scissors! Player Wins!");
wins++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if (Computer == Paper){
JOptionPane.showMessageDialog(null,"Rock v Paper! Computer Wins!");
losses++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player Wins
if(Player == Paper)
if(Computer == Rock){
JOptionPane.showMessageDialog(null,"Paper v Rock! Player Wins!");
wins++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer Wins
else if (Computer == Scissors){
JOptionPane.showMessageDialog(null,"Paper v Scissors! Computer Wins!");
losses++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
}
if(wins>losses){
JOptionPane.showMessageDialog(null,"The Player Wins!");
}if(losses>wins){
JOptionPane.showMessageDialog(null,"The Computer Wins!");
}
}
case 2 :
{
JOptionPane.showMessageDialog(null,"You have selected the game's rules.\nOpening the rules...\nLoaded successfully! Please press OK.");
JOptionPane.showMessageDialog(null,"Here are the rules for the game:\nIn total, there are 3 rounds for this game, including Ties.\nPaper vs Rock => Paper is the Winner. Add 1 to the winner.\nPaper vs Scissors => Scissors is the Winner. Add 1 to the winner.\nRock vs Scissors => Rock is the Winner. Add 1 to the winner.\nRock vs Rock => Tie. No score.\nScissors vs Scissors => Ties. No score.\nPaper vs Paper => Tie. No score.");
}
case 3 :
{
JOptionPane.showMessageDialog(null,"3");
}
default :
JOptionPane.showMessageDialog(null,"You entered an invalid operation. Please select 1, 2, or 3.");
String input2 = JOptionPane.showInputDialog("Would you like to return to the main menu? Please respond with Yes or No.");
if (input2.equalsIgnoreCase ("No"))
JOptionPane.showMessageDialog(null,"You have selected to exit the program. Closing the program... please press OK.");
{
i=1;
}
}
}
You need to use break at the end of every case. Syntax is like:
switch(variable){
case 1:
//Do your operations.
break;
case 2:
//Do your operations.
break;
.
.
.
}
Let me know if this helps.
The code is below. My program runs smoothly except for two issues. It does not play for just three rounds and I need it to play for just 3 rounds and determine a winner based off of whoever won at least once during the three rounds despite ties (if it comes down to that scenario randomly). It plays until there is a 2 to 1 winning ratio for either the player or the computer. I do not want it to run continuously like this. The other issue is that once a final winner is declared, the line “Enter rock, paper, or scissors” appears again, and it is not needed there. How do I go about fixing these two issues? Thank you.
import java.util.Scanner;
import java.util.Random;
/**
*
* #author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Prompt user to enter rock, paper, or scissors
//Compare random value
while(true){
int wins = 0;
int losses = 0;
int rnd = 0;
int USER = 0;
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n"
+ "Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
// Plays 3 rounds before terminating
while(rnd<3) {
Random GAME = new Random();
int PC = 1+GAME.nextInt(3);
Scanner keyboard = new Scanner (System.in);
int SCISSOR, ROCK, PAPER;
ROCK = 1;
PAPER = 2;
SCISSOR= 3;
String USER_Input = keyboard.next();
if(USER_Input.equals("Rock")) {
USER = 1;
}
if(USER_Input.equals("Paper")) {
USER = 2;
}
if(USER_Input.equals("Scissors")) {
USER = 3;
}
//If the user enters a value greater then 3 or less than 1 it will terminate the program
//and display an error message
while (USER > 3 || USER < 1) {
System.err.println("Incorrect value entered.");
System.exit(0);
break;
}
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player wins
if(USER == SCISSOR)
if(PC == PAPER){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer wins
else if(PC == ROCK){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player wins
if(USER == ROCK)
if(PC == SCISSOR ){
System.out.println("Rock v Scissor! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer wins
else if (PC == PAPER){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player Wins
if(USER == PAPER)
if(PC == ROCK){
System.out.println("Paper v Rock! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer Wins
else if (PC == SCISSOR){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
}
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YN = new Scanner(System.in);
String YN_String = YN.next();
if(YN_String.equals("Yes") || YN_String.equals("yes")){
}if(YN_String.equals("No") || YN_String.equals("no")) {
System.out.println ("Goodbye!");
break;
}
}
}
}
The code looks like it should work the way you want it to, aside from the fact that in the case of ties, you aren't incrementing the round counter:
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
rnd++;
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
rnd++;
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
rnd++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
As a side note, you could take out the rnd++; from each individual result, and have it increase every time you get input from the user, since you want it to increase no matter what.
Your second issue stems from the fact that you're prompting for new input every time you receive input, after you tell the user the result, including the ending result. You could fix this by removing the prompt command from the individual results and placing it at the beginning of your while loop instead. Note that if you do this, you may also want to remove the prompt from your welcome message as well, or it will prompt twice at the beginning: once in the welcome message, and again when the while loop starts immediately after.
import java.util.Scanner;
import java.util.Random;
/**
*
* #author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Set integers for wins, losses, rounds, and user
while(true){
int wins = 0;
int losses = 0;
int round = 0;
int Player = 0;
//Plays 3 rounds before terminating
//Prompt user to input Rock Paper Scissors
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");
Scanner keyboard = new Scanner (System.in);
while(round<3) {
System.out.println("Enter \"Rock\", \"Paper\" or \"Scissors\"");
Random Game = new Random();
int Computer = 1+Game.nextInt(3);
int Scissors, Rock, Paper;
Rock = 1;
Paper = 2;
Scissors= 3;
String UserInput = keyboard.next();
if(UserInput.equals("Rock")) {
Player = 1;
}
if(UserInput.equals("Paper")) {
Player = 2;
}
if(UserInput.equals("Scissors")) {
Player = 3;
}
//If the user enters a value greater then 3 (Scissors) or less than 1 (Rock)
//it will terminate the program and display an error message
while (Player > 3 || Player < 1) {
losses++;
round++;
System.out.println("Not a valid input! Computer wins");
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish tie scenarios using if statements
if(Player == Computer){
if(Player == Scissors){
System.out.println("Scissors v Scissors! Tie!");
round++;
}
if(Player == Rock){
System.out.println("Rock v Rock! Tie!");
round++;
}
if(Player == Paper){
System.out.println("Paper v Paper! Tie!");
round++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish the various winning scenarios using if and else if statements
//Player wins
if(Player == Scissors)
if(Computer == Paper){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if(Computer == Rock){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player wins
if(Player == Rock)
if(Computer == Scissors ){
System.out.println("Rock v Scissors! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if (Computer == Paper){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player Wins
if(Player == Paper)
if(Computer == Rock){
System.out.println("Paper v Rock! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer Wins
else if (Computer == Scissors){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
}
//Determine final winner using if statements
//Ask if player would like to play again by setting up string
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YesNo = new Scanner(System.in);
String YesNo_String = YesNo.next();
if(YesNo_String.equalsIgnoreCase("yes")) {
}if(YesNo_String.equalsIgnoreCase("No")) {
System.out.println ("Goodbye!");
}
}
}
}
This is my code:
/* Name: Steven Royster
* Date: Jan. 15, 2015
* Rock, Paper, Scissors Program
* This program simulates a game of rock, paper, scissors with the user until someone has one a total of five times.
*/
import java.util.Random;
import java.util.Scanner;
public class RPS {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Let's play rock, paper, scissors! First to five wins!");
String[] choice = { "zero" , "rock" , "paper" , "scissors" };
Random rander = new Random();
Scanner input = new Scanner(System.in);
int userScore = 0, compScore = 0,
userChoice, compChoice;
while (compScore < 5 && userScore < 5)
{
compChoice = rander.nextInt(3) + 1;
System.out.println("\nEnter: 1 for ROCK | 2 for PAPER | 3 for SCISSORS.");
userChoice = input.nextInt();
if (compChoice == userChoice) // tie
{
System.out.println("I chose " + choice[compChoice] + " too, so we tied!");
}
else if ( ( compChoice == 1 && userChoice == 3 ) //computer wins
|| ( compChoice == 2 && userChoice == 1 )
|| ( compChoice == 3 && userChoice == 2) )
{
System.out.println("I win! I chose " + choice[compChoice] + ". " +
choice[compChoice] + " beats " + choice[userChoice] + "." );
compScore += 1;
}
else //human wins
{
System.out.println("You win! I chose " + choice[compChoice] + ". " +
choice[userChoice] + " beats " + choice[compChoice] + ".");
userScore += 1;
}
}//end while
if (userScore == 5)
{
System.out.println("\nCongrats! You're the winner! You got "
+ userScore + " points. I only got " + compScore + " points." );
}
}//end main
}//end class
Is there a better algorithm I can implement rather than using the three separate conditions within my else-if statement? Also, instead of having the 'zero' string in my array, how could I check through my array using only numbers 1, 2, and 3?
Just use
String[] choice = { "rock" , "paper" , "scissors" };
Then instead of choice[userChoice] you can do choice[userChoice - 1].
You don't need to write
if ( ( compChoice == 1 && userChoice == 3 )
|| ( compChoice == 2 && userChoice == 1 )
|| ( compChoice == 3 && userChoice == 2) )
because it is the same as
if (compChoice == 1 + (userChoice % 3))
i had a quick question, so i finished up writing my program which is a simple Rock, Paper Scissors game but at the moment it only displays 0,1,2 which 0 is scissors, 1 is rock, and 2 is paper. The problem im having is that im not quite sure how to give the users input as well as what the computer outputs go from displaying numbers to displaying scissors, rock, paper.
import java.util.Scanner;
import java.lang.Math;
public class Lab3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Hello user! lets play ");
System.out.println("Rock, Paper, Scissors.");
System.out.println();
System.out.print("Type in 0 for Scissors, 1 for Rock, or 2 for Paper ");
int userInput = in.nextInt();
int opponentsHand = (int)(Math.random()*3);
if (userInput == opponentsHand)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" Despite your efforts of playing " + userInput + ", this battle has ended in a draw!");
}
if (userInput < opponentsHand && opponentsHand != 2)
{
System.out.print("Darth vader has played "+ opponentsHand);
System.out.println(", You played " + userInput + "You have Lost");
}
else if (userInput < opponentsHand && opponentsHand == 2)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You played " + userInput + " You have won");
}
if (userInput > opponentsHand && opponentsHand != 0)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You have played " + userInput + " You have won");
}
else if (userInput > opponentsHand && opponentsHand == 0)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You have played " + userInput + " You have lost");
}
}
}
thank you
One way is to simply use an array to store the names of the hands:
String[] hands = {"scissors", "rock", "paper"};
When you print, you do the following:
System.out.print("Darth Vader has played " + hands[opponentsHand]);