So I made this rock paper scissors game a while ago using Java and I found a little bug I forgot to fix when I showed it to my friend today. Basically, the code takes a user input (i.e rock, paper, or scissors) and if their input does not equal rock, paper, or scissors, the main while loop is broken and the game is stopped.
How can I make it so that not only does it break the loop, but restarts it as well? I want to do this so that when someone gives an invalid input it automatically restarts so the player doesn't need to run the program all over again.
Here's the code for my main class:
public class rps {
public static void main(String []args){
boolean gameRunning = true;
while(gameRunning) {
System.out.println("Do you choose rock, paper, or scissors?");
UserChoice userInput = new UserChoice();
String userChoice = userInput.userDecide();
if(userChoice != "rock" || userChoice != "paper" || userChoice != "scissors") {
System.out.println("'" + userChoice + "'" + " is not a valid choice.");
System.out.println("Please choose between rock, paper, or scissors.");
gameRunning = false;
break;
// this is where I want to restart the function
}
System.out.println("You threw: " + userChoice);
ComputerInput computerChoice = new ComputerInput();
String computerInput = computerChoice.computerDecide();
System.out.println("Computer threw: " + computerInput);
CompareChoices compareResults = new CompareChoices();
gameRunning = compareResults.compare(userChoice, computerInput);
}
}
};
UPDATE
I figured out a few of the problems thanks to some help from the nice people on here. I used && instead of || (which is dumb because I originally used && anyways -_-), I used the "continue" statement instead of break, I removed gameRunning = false;, and I changed the way userChoice was compared to the valid responses.
Instead of comparing it to Strings like "rock" and "paper", I created an array
(String validChoices[] = {"rock", "paper", "scissors"};) which holds the valid responses. Then I compared userChoice to the indices of the array.
Here is my new code:
public class rps {
public static void main(String []args){
boolean gameRunning = true;
while(gameRunning) {
System.out.println("Do you choose rock, paper, or scissors?");
UserChoice userInput = new UserChoice();
String userChoice = userInput.userDecide();
String validChoices[] = {"rock", "paper", "scissors"};
if(!userChoice.equals(validChoices[0]) && !userChoice.equals(validChoices[1]) && !userChoice.equals(validChoices[2])) {
System.out.println("'" + userChoice + "'" + " is not a valid choice.");
System.out.println("Please choose either rock, paper, or scissors.");
continue;
}
System.out.println("You threw: " + userChoice);
ComputerInput computerChoice = new ComputerInput();
String computerInput = computerChoice.computerDecide();
System.out.println("Computer threw: " + computerInput);
CompareChoices compareResults = new CompareChoices();
gameRunning = compareResults.compare(userChoice, computerInput);
}
}
};
Thanks everyone!
Do not set gameRunning to false, to not use break, use continue, which will ignore the rest of the loop and start the loop again.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
A couple of problems. setting your loop condition to false and breaking leaves no way to restart the loop. Also, as #resueman hinted at. Use .equals() instead of == for comparing strings. My suggestion is to put the error checking inside another while loop.
while(!userChoice.equals("rock") && !userChoice.equals("paper") && !userChoice.equals("scissors")) {
System.out.println("Your choice was invalid, try again");
userChoice = userInput.userDecide();
}
Also, you should maybe declare some of this stuff outside your while loop (I am thinking of your ComputerInput, UserInput and CompareChoices objects).
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
public class rps {
public static void main(String args[]) {
int userCount = 0;
int myCount = 0;
int tieCount = 0; /* All 3 are counters that keep track of win scores */
Scanner scan = new Scanner(System.in); /* Create a scanner */
String play = ""; /* Scanner string local variable */
do {
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
play = scan.nextLine(); //
}
while (play.equalsIgnoreCase("play")); {
System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
String rps = scan.nextLine();
while (rps.equals('R') || rps.equals('P') || rps.equals('S')) {
System.out.println("You chose: " + rps);
}
int rand = (int)(Math.random() * 3);
String myMove = "";
if(rand == 0) {
myMove = "Rock";
}
else if(rand == 1) {
myMove = "Paper";
}
else {
myMove = "Scissors";
}
System.out.println("I chose: " + myMove);
if(rps.equals(myMove)) {
System.out.println("Tie!");
tieCount++;
}
else if(rps.equals('P') && myMove.equals('S')) {
System.out.println("Scissors beats paper, a win for me!");
myCount++;
}
else if(rps.equals('S') && myMove.equals('R')) {
System.out.println("Rock beats scissors, a win for me!");
myCount++;
}
else if(rps.equals('R') && myMove.equals('P')) {
System.out.println("Paper beats rock, a win for me!");
myCount++;
}
else if(rps.equals('S') && myMove.equals('P')) {
System.out.println("Scissors beats paper, a win for you!");
userCount++;
}
else if(rps.equals('R') && myMove.equals('S')) {
System.out.println("Rock beats scissors, a win for you!");
userCount++;
}
else if(rps.equals('S') && myMove.equals('P')) {
System.out.println("Paper beats rock, a win for you!");
userCount++;
}
System.out.println("Please enter Play if you want to play the game again or anything else to Stop.");
if(!play.equalsIgnoreCase("play")) break;
}
}
How can I make it so I can end the loop? It's giving me an error that it cannot be used outside of a loop but I am confused because it is still in my loop? I assume it is something to do with the fact that it is in a main method but without the main method I get a different error on the part of my scanner code. (I am supposed to make a rock paper scissors game that ends at the end of the loop when the user chooses to stop. I don't know how to go about fixing this error.)
The break is indeed outside the loop. Your loop is this:
do {
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
play = scan.nextLine(); //
} while (play.equalsIgnoreCase("play"));
You probably meant your code to be like this:
// no do {
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
play = scan.nextLine(); //
// no }
while (play.equalsIgnoreCase("play")) { // no semicolon!
...
}
UPDATE
You seem to be confused by the two forms the while loop can take:
do { <code> } while ( <condition> ) - here, the code comes before the condition and is always executed at least once.
while ( <condition>) { <code> } - here, the condition comes first, and the code might not be executed even once.
Your example should look something like this to work:
Scanner scan = new Scanner(System.in); /* Create a scanner */
while (true) {
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
String play = scan.nextLine();
if (!play.equalsIgnoreCase("play")) {
break();
}
System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
...
}
It looks like you want to read a first value before looping. So do this :
//do { // no need do
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
play = scan.nextLine(); //
// } no need do
while (play.equalsIgnoreCase("play")) { // remove comma here
System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
S
Final code wil be :
// these two lines allow to initialize "play" variable
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
play = scan.nextLine();
while (play.equalsIgnoreCase("play")) { // (**) test "play" var to keep in the loop
// these 2 lines are good.
System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
String rps = scan.nextLine();
...
...
...
...
...
...
// at the end of the loop, read again "play" var to decides if you stay or stop loop
System.out.println("Please enter Play if you want to play the game again or anything else to Stop.");
play = scan.nextLine(); // IMPORTANT
// but no need to break, because the play value will be tested by the enclosing while condition at (**)
// if(!play.equalsIgnoreCase("play"))
// break;
}
Your break is actually not in a loop. You should have a look at the syntax of do-while loops. Then, you can realise that the loop ends after the semicolon after while().
do {
// Do some stuff in a loop...
}
while(condition); // <-- Loop ends here
The relevant difference between a while loop and a do-while loop is that the former one is only entered when the given condition elevates to true while the latter one is always entered at least one.
while
Check condition whether statement in loop should be executed
Execute statement in loop and go to beginning
do-while
Execute statement in loop
Check condition to potentially start again.
You have a do-while loop.
do {
System.out.print("Please enter Play if you want to play
the game or anything else to Stop");
play = scan.nextLine(); //
} while (play.equalsIgnoreCase("play"))
And you don't have a break within this loop.
Your break statement is out of loop. In short, you may need to learn how to use loop from a good book.
So here i am trying to create a program that takes an input as an int and then plays a game of Rock paper scissors. It seems to want to reprint statements that it shouldn't be and is skipping printing statements as well. I would love some assistance if possible. I have tried setting up print statements everywhere but it has just been more confusing.
import java.util.Scanner;
public class RPSS{
//Main method
public static void main(String[ ] argc)
{
System.out.println("Lets play rock paper scissors");
Scanner tnt = new Scanner(System.in);
String computerHand; // string variable for computer choice
String userHand; // string variable for user choice
//
String answer = "";
while (!a
nswer.equals("No") && (!answer.equals("no"))){
userHand = userHand();
computerHand = computerHand();
System.out.println("The User picks " + userHand + " " );
System.out.print("The Computer picks " + computerHand );
String winner = getWinner(computerHand, userHand);
System.out.println(winner);
System.out.println("play again?");
answer = tnt.next();
}
//Condition for the do-while loop
}
public static String userHand(){ //method for users choice in the game
//prints message to user giving them choices
System.out.println(" ");
System.out.println("1. Rock ");
System.out.println("2. Paper ");
System.out.println("3. Scissors ");
int userChoice; // user choice variable in this method
Scanner tnt = new Scanner(System.in); // creates instance of scanner class
userChoice = tnt.nextInt(); //reads user input
return getChoice(userChoice); //returns user choice to userChoice
}
public static String computerHand() //method for computer generated choice
{
int computernum = 1 + (int)(Math.random() * (( 2) +1));
return getChoice(computernum);
}
public static String getChoice(int num) //method recieving both computer hand and user hand
{
// if statements to place the correct choice
String choice = "";
if (num == 1){
choice = "Rock";
}
else if(num == 2){
choice = "Paper";
}
else if(num == 3){
choice = "Scissors";
}
return choice;
}
// Method determing the winner
public static String getWinner(String computerChoice, String userChoice)
{
computerChoice = computerHand(); //places computerChoice variable in computerhand
userChoice = userHand(); //does same for user choice
String winner="";
if (userChoice.equals("Rock") && computerChoice.equals("Paper")){
System.out.println("The computer wins");
return winner;
}
else if (userChoice.equals("Paper") && computerChoice.equals("Scissors")){
System.out.println(" The computer wins");
return winner;
}
else if (userChoice.equals("Scissors") && computerChoice.equals("Rock")){
System.out.println(" The computer wins ");
return winner;
}
else if (userChoice.equals("Rock") && computerChoice.equals("Paper")){
System.out.println(" The computer wins ");
return winner;
}
else if(userChoice.equals(computerChoice))
{
System.out.println(" There is no winner");
return " ";
}
else{
return winner;
}
}
}
The first problem is that userhand() and computerHand() are being called twice per "round", once at the beginning of the while loop inside the main method and once at the beginning of the getWinner() method. Elimination of the calls at the beginning of the getWinner() method should solve the repeats.
The 2nd Problem is that instead of modifying the value of winner inside the getWinner() method before returning it, you are you are simply outputting the message via println(). an example of fixing this would be converting this:
if (userChoice.equals("Rock") && computerChoice.equals("Paper"){
System.out.println("The computer wins");
return winner;
}
to this:
if (userChoice.equals("Rock") && computerChoice.equals("Paper")){
winner = "The computer wins";
return winner;
}
another minor issue is the fact that
userChoice.equals("Rock") && computerChoice.equals("Paper")
is checked twice, id just remove the entire if else block based around the
2nd check of it
Lastly i would treat the final else clause as the player wins one and set winner to something like " The player wins "
Sorry I am new to this site so not sure how this will show up. I am trying to make a simple Rock, Paper, Scissors game. After the while statement, if R, P, S isn't entered, the program just does nothing. I want it to loop back to the question at the beginning so a right choice can be entered. Also, how would I enter a print statement like "Invalid Choice Please Retry"?
package rps.gameapp;
import java.util.Scanner;
public class RPSGameApp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String userChoice;
String playAgain;
int randNum = (int) (Math.random() * 3);
do
{
System.out.println("Welcome to Rock, Paper, Scissors Game.");
System.out.println("Pick R, P, or S.");
userChoice = sc.nextLine();
while (!userChoice.equalsIgnoreCase("P")
&& !userChoice.equalsIgnoreCase("R")
&& !userChoice.equalsIgnoreCase("S"));
String compChoice = "";
switch (randNum)
{
case 0:
compChoice = "R";
break;
case 1:
compChoice = "P";
break;
case 2:
compChoice = "S";
break;
}
System.out.println("The computer entered \"" + compChoice + "\".");
if (compChoice.equalsIgnoreCase(userChoice))
{
System.out.println("Draw");
} else if (userChoice.equalsIgnoreCase(userChoice)
&& compChoice.equalsIgnoreCase("S")
|| userChoice.equalsIgnoreCase("P")
&& compChoice.equalsIgnoreCase("R")
|| userChoice.equalsIgnoreCase("S")
&& compChoice.equalsIgnoreCase("P"))
{
System.out.println("User Wins");
} else
{
System.out.println("User Loses");
}
System.out.print(
"Do you want to play again? (Y/N)");
playAgain = sc.nextLine();
} while (playAgain.equalsIgnoreCase("Y"));
System.out.println("Thanks for Playing!");
}
}
It looks like you forgot one do for your inner do while loop.
It should be :
do {
do {
System.out.println("Welcome to Rock, Paper, Scissors Game.");
System.out.println("Pick R, P, or S.");
userChoice = sc.nextLine();
} while (!userChoice.equalsIgnoreCase("P") && !userChoice.equalsIgnoreCase("R") && !userChoice.equalsIgnoreCase("S"));
...
} while (playAgain.equalsIgnoreCase("Y"));
Without that inner do (and the curly braces surrounding that loop's body), the inner loop becomes a while loop with an empty body.
Like Eran said, you need to wrap your do-while loop in another loop, that will keep asking user for correct input. This is fully working code. One thing that could be better is the message after user inputs wrong letter.
Edit: also make sure you draw random number for every iteration.
Edit 2: to change the message depending on user input you can introduce a new variable that will keep the track of number of times you asked user for correct input. If it is 0- it means user is asked the first time and we should print "Welcome" message. It is anything other than 0- you need to ask the user for correct input. After every round we assign zero to the variable again and the cycle repeats. I have implemented this change in the code. Note that this variable can also be a boolean.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String userChoice;
String playAgain;
int iterationNumber;
while (true) {
iterationNumber = 0;
do {
if (iterationNumber == 0) {
System.out.println("Welcome to Rock, Paper, Scissors Game.");
System.out.println("Pick R, P, or S.");
} else {
System.out.println("Please enter valid letter.");
System.out.println("Pick R, P, or S.");
}
iterationNumber++;
userChoice = sc.nextLine();
} while (!userChoice.equalsIgnoreCase("P")
&& !userChoice.equalsIgnoreCase("R")
&& !userChoice.equalsIgnoreCase("S"));
String compChoice = "";
int randNum = (int) (Math.random() * 3);
switch (randNum) {
case 0:
compChoice = "R";
break;
case 1:
compChoice = "P";
break;
case 2:
compChoice = "S";
break;
}
System.out.println("The computer entered \"" + compChoice + "\".");
if (compChoice.equalsIgnoreCase(userChoice)) {
System.out.println("Draw");
} else if (userChoice.equalsIgnoreCase("R")
&& compChoice.equalsIgnoreCase("S")
|| userChoice.equalsIgnoreCase("P")
&& compChoice.equalsIgnoreCase("R")
|| userChoice.equalsIgnoreCase("S")
&& compChoice.equalsIgnoreCase("P")) {
System.out.println("User Wins");
} else {
System.out.println("User Loses");
}
System.out.print(
"Do you want to play again? (Y/N)");
playAgain = sc.nextLine();
if (playAgain.equalsIgnoreCase("N")) {
break;
}
iterationNumber = 0;
}
System.out.println("Thanks for Playing!");
}
I made this Rock Paper scissors game but i cnt figure out how to make it show invalid error when the user enters something other than R,P,S. It would be helpful if any once could tell me how to make it so that it does this. Im a relatively new coder. Thanks in advance for all your help
import java.util.Random;
import java.util.Scanner;
public class RPS {
public static void main(String[] args)
{
String userPlay; //User's play -- "R", "P", or "S"
String computerPlay = ""; //Computer's play -- "R", "P", or "S"
int computerInt;
String response;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
System.out.println("Lets play Rock, Paper, Scissors!\n" +
"Choose your move.\n" + "Rock = R, Paper" +
"= P, and Scissors = S.");
System.out.println();
//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3)+1;
//Translate computer's randomly generated play to
//string using if //statements
if (computerInt == 1)
computerPlay = "R";
else if (computerInt == 2)
computerPlay = "P";
else if (computerInt == 3)
computerPlay = "S";
//Get player's play from input-- note that this is r
System.out.println("Enter your play: ");
userPlay = scan.next();
//Make player's play uppercase
userPlay = userPlay.toUpperCase();
//Print computer's play
System.out.println("Your opponents play is: " + computerPlay);
//See who won.
if (userPlay.equals(computerPlay))
System.out.println("It's a tie!");
else if (userPlay.equals("R"))
if (computerPlay.equals("S"))
System.out.println("Rock breaks scissors. You win!!");
else if (computerPlay.equals("P"))
System.out.println("Paper covers rock. You lose!!");
else if (userPlay.equals("P"))
if (computerPlay.equals("S"))
System.out.println("Scissor cuts paper. You lose!!");
else if (computerPlay.equals("R"))
System.out.println("Paper covers rock. You win!!");
else if (userPlay.equals("S"))
if (computerPlay.equals("P"))
System.out.println("Scissor cuts paper. You win!!");
else if (computerPlay.equals("R"))
System.out.println("Rock breaks scissors. You lose!!");
else
System.out.println("Invalid user input.");
}
}
I think the best way to do it is with loop. You ask to enter player's move, if it is unacceptable you ask again. You continue it until user enters valid string. In code it will look like this
//Get player's play from input
boolean moveOk = false;
while (moveOk == false) {
System.out.println("Enter your play: ");
userPlay = scan.next();
//Make player's play uppercase
userPlay = userPlay.toUpperCase();
// check that the input is ok
if ("R".equals(userPlay) ||
"P".equals(userPlay) ||
"S".equals(userPlay)) {
moveOk = true;
} else {
System.out.println("Bad input, try again!");
}
}
If you want to show error and then stop the program, instead of creating the loop you can use simple if with similar condition and when input is incorrect print error message and use return.
Also you could make your code easier to understand if you divide your conditions in parts, for example by user input. See example below.
//See who won.
String result = "";
if (userPlay.equals(computerPlay)) {
result = "It's a tie!";
}
if (userPlay.equals("R")) {
if (computerPlay.equals("S")) {
result = "Rock breaks scissors. You win!!";
}
if (computerPlay.equals("P")) {
result = "Paper covers rock. You lose!!";
}
}
if (userPlay.equals("P")) {
if (computerPlay.equals("S")) {
result = "Scissor cuts paper. You lose!!";
}
if (computerPlay.equals("R")) {
result = "Paper covers rock. You win!!";
}
}
if (userPlay.equals("S")) {
if (computerPlay.equals("P")) {
result = "Scissor cuts paper. You win!!";
}
if (computerPlay.equals("R")) {
result = "Rock breaks scissors. You lose!!";
}
}
System.out.println(result);
Also you could use switch statement.
My question is not about general game algorithm. When i answer "true" to the question "Do you want to continue? Enter true or false.", program outputs:
"Player one, throw your shape(rock, paper, scissors)!
Player two, throw your shape(rock, paper, scissors)!"
So i can enter values in first cycle but not in second. How can i fix it? Thanks.
code:
public static void main(String[] args) {
String playerOne;
String playerTwo;
Scanner myScan = new Scanner(System.in);
boolean playAgain = true;
do
{
System.out.println("Player one, throw your shape(rock, paper, scissors)! ");
playerOne = myScan.nextLine();
System.out.println("Player two, throw your shape(rock, paper, scissors)! ");
playerTwo = myScan.nextLine();
if (playerOne.equals("scissors") && playerTwo.equals("rock"))
{
System.out.println("Player one lost! Playr two won!");
}
else if (playerOne.equals("scissors") && playerTwo.equals("paper"))
{
System.out.println("Player one won! Player two lost!");
}
else if (playerOne.equals("rock") && playerTwo.equals("paper"))
{
System.out.println("Player one lost! Player two won!");
}
else if (playerOne.equals("rock") && playerTwo.equals("scissors"))
{
System.out.println("Player one won! Player two lost!");
}
else if (playerOne.equals("paper") && playerTwo.equals("scissors"))
{
System.out.println("Player one lost! Player two won!");
}
else if (playerOne.equals("paper") && playerTwo.equals("rock"))
{
System.out.println("Player one won! Player two lost!");
}
System.out.println("Do you want to continue? Enter true or false.");
playAgain = myScan.nextBoolean();
}while(playAgain);
}
}
This is because the Scanner only reads the boolean you request with nextBoolean and nothing else, not even the following line ending. The line ending then remains in the input stream and is returned as an empty line when the loop comes around and you ask for nextLine.
What you'll want to do instead is to read a complete line instead of just a boolean for playAgain, and interpret that manually (suggestedly with something like myScan.nextLine().equals("yes")).