Java Programming - Beginner for Paper Rocks Scissors - java

Need Some Guidance if possible please. New to Java. Not to sure why code below for rocks, papers , scissors game is not working. Any suggestions please or advice would be most welcome. I know there are plenty of examples on this great forum - but i am still grasping the basics. Thank you.
import java.util.*;
import java.util.Scanner;
import java.util.Random;
public class Game
{
public static void main (String[] args){
int NumberofRoundsPlayed;
int NumberofRoundsWonbyHuman = 0;
Scanner Keyboard = new Scanner (System.in
System.out.println("DO YOU WANT TO PLAY ROCK PAPER SCISSORS- Y/N");
String HumanPlaying = Keyboard.nextLine();
if(HumanPlaying ==("No"))
{
System.out.println("Game Over");
System.exit(0);}
}
int Paper = 1;
int Scissor = 2;
int Rock = 3;
int HumanSelection;
int humanroundsWon =0;
System.out.println("HOW MANY ROUNDS DO YOU WANT TO PLAY");
NumberofRoundsPlayed = Keyboard.nextInt();
while (NumberofRoundsPlayed <= NumberofRoundsPlayed +1)
{
Scanner Computer = new Scanner (System.in);
Random rand = new Random();
int ComputerChoice =Computer.nextInt(3)+1;
System.out.println(ComputerChoice);
System.out.println("Select 1 for Paper, 2 for Scissor or 3 for Rock");
HumanSelection = Keyboard.nextInt();
//SEE WHO WINS
If (ComputerChoice== 1)
{
If (HumanSelection==1)
{
System.out.println("Computer and Human Have Tied");
} Else if (HumanSelection==2)
{
System.out.println("Person Wins");
humanroundsWon == humanroundsWon+1;
}
Else if (HumanSelection==3)
{
System.out.println("Computer Wins");
}
Else if (ComputerChoice==2)
{
If (HumanSelection==1)
{
System.out.println("computer Wins");
}
Else if (HumanSelection==2)
{
System.out.println("Computer and Person Have Tied");
}
Else if (HumanSelection==3)
{
System.out.println("Person Wins");
humanroundsWon == humanroundsWon+1;
}
}
Else if (ComputerChoice==3)
{
}
If (HumanSelection==1)
{
System.out.println("Person Wins");
humanroundsWon == humanroundsWon+1;
}
Else if (HumanSelection==2)
{
{ System.out.println("Computer Wins");
}
Else if (HumanSelection==)
{
System.out.println("Tie");
roundsWon == roundsone+1;
}
}
}
}
}
}
System.out.println("Game Over");
System.exit(0);}

If it won't compile, Scanner Keyboard = new Scanner (System.in is an incomplete statement.

here is the code, if my understanding of your scenario is correct. When you ask questions, indicate what the code actually need to perform, so that others can help you. And a little tip, try to use an IDE like eclipse which will help you in coding. you can find it here just download Eclipse IDE for Java EE Developers according to your platform architecture( 32bit or 64bit) and extract the zip file. Then run the eclipse IDE it is simple as that
import java.util.*;
import java.util.Scanner;
import java.util.Random;
public class Game
{
public static void main (String[] args){
int NumberofRoundsPlayed;
int NumberofRoundsWonbyHuman = 0;
Scanner Keyboard = new Scanner (System.in);
System.out.println("DO YOU WANT TO PLAY ROCK PAPER SCISSORS- Y/N");
String HumanPlaying = Keyboard.nextLine();
if(HumanPlaying.equals("No"))
{
System.out.println("Game Over");
System.exit(0);
}
int Paper = 1;
int Scissor = 2;
int Rock = 3;
int HumanSelection;
int humanroundsWon =0;
System.out.println("HOW MANY ROUNDS DO YOU WANT TO PLAY");
NumberofRoundsPlayed = Keyboard.nextInt();
while (NumberofRoundsPlayed < NumberofRoundsPlayed++)
{
Scanner Computer = new Scanner (System.in);
Random rand = new Random();
int ComputerChoice =Computer.nextInt()+1;
System.out.println(ComputerChoice);
System.out.println("Select 1 for Paper, 2 for Scissor or 3 for Rock");
HumanSelection = Keyboard.nextInt();
//SEE WHO WINS
if (ComputerChoice== 1)
{
if (HumanSelection==1)
{
System.out.println("Computer and Human Have Tied");
}
else if (HumanSelection==2)
{
System.out.println("Person Wins");
humanroundsWon++;
}
else if (HumanSelection==3)
{
System.out.println("Computer Wins");
}
}
else if (ComputerChoice==2)
{
if (HumanSelection==1)
{
System.out.println("computer Wins");
}
else if (HumanSelection==2)
{
System.out.println("Computer and Person Have Tied");
}
else if (HumanSelection==3)
{
System.out.println("Person Wins");
humanroundsWon++;
}
}
else if (ComputerChoice==3)
{
if (HumanSelection==1)
{
System.out.println("Person Wins");
humanroundsWon++;
}
else if (HumanSelection==2)
{
System.out.println("Computer Wins");
}
else if (HumanSelection==3)
{
System.out.println("Tie");
}
}
}
System.out.println("Game Over");
System.exit(0);
}
}

Related

How can I start the program over again

I'm trying to start the game over if the user enters "Yes". I tried using a while loop, but it's gone wrong somewhere that I can't find.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
int playerScore = 0;
int computerScore = 0;
int round = 0;
String decision;
// Get user input
while (round<3) {
System.out.println("Please enter your move: Rock, Paper or Scissors");
System.out.println(">>");
Scanner input = new Scanner(System.in);
String playerMove = input.next();
// Check if user input is valid
if (!playerMove.equalsIgnoreCase("Rock") && !playerMove.equalsIgnoreCase("Paper") && !playerMove.equalsIgnoreCase("Scissors")) {
System.out.println("Move is invalid, Opponent gets a point");
computerScore++;
round++;
System.out.println("Your point is "+ playerScore + "; Opponent score is "+ computerScore);
} else {
// Randomly generate computerMove
int computerInt = (int)(Math.random()*3);
String computerMove = " ";
if (computerInt == 0) {
computerMove = "Rock";
} else if (computerInt == 1) {
computerMove = "Paper";
} else if (computerInt == 2) {
computerMove = "Scissors";}
System.out.println("Opponent move is "+ computerMove);
// Establish winning or losing scenarios
if (playerMove.equalsIgnoreCase(computerMove)) {
System.out.println("Tied");
round++;
System.out.println("Your point is "+ playerScore + "; Opponent score is "+ computerScore);
} else if (playerMove.equalsIgnoreCase("Rock") && computerMove.equalsIgnoreCase("Scissors") ||
playerMove.equalsIgnoreCase("Scissors") && computerMove.equalsIgnoreCase("Paper") ||
playerMove.equalsIgnoreCase("Rock") && computerMove.equalsIgnoreCase("Paper")) {
System.out.println("You won");
playerScore++;
round++;
System.out.println("Your point is "+ playerScore + "; Opponent score is "+ computerScore);
}
else {
System.out.println("You lost");
computerScore++;
round++;
System.out.println("Your point is "+ playerScore + "; Opponent score is "+ computerScore);
}
}
// Determine the last winner
if (playerScore < computerScore) {
System.out.println("You lose, so sad ;(");
} else if (playerScore > computerScore) {
System.out.println("You win, here's a cookie ;) ");
} else {
System.out.println("Tied, maybe try harder ^_^"); }
}
System.out.println("Do you wanna play again?");
}
}
}
Sounds like this might be a HW or Test question, so just writing steps to help you out:
first have a user-decision variable and initialize it to "Y"
then put the game in a while(user-decision="Y") loop
at the end of this loop ask the user for their decision and update the user-decision variable to either Y or N
if they say Y loop continues, else it ends
if you need to repeat your code, put it in a new class (Game) and instantiate in main () and call until your Game class returns TRUE (press Y)
package appMain;
import java.util.Scanner;
public class Game {
public Boolean PlayGame() {
System.out.println("START GAME");
//
// >> PUT THERE YOUT GAME CODE <<
//
Scanner input = new Scanner(System.in);
String answer = input.next();
if(answer.equals("Y")) {
return true;
}
return false;
}
}
package appMain;
public class GameMain {
public static void main(String args[]) {
Game game = new Game();
while (game.PlayGame()) {
}
System.out.println("GAME finish");
}
}

java beginner rock paper scissors game

I am working on a "Rock, Paper, Scissors" game for my intro java class. Here is the prompt: Create a game of "Rock, Paper, Scissors" where the computer randomly chooses rock, paper, or scissors. Let the user enter a number of 1, 2, or 3, each representing one of three choices. Determine a winner. Game should ask the user to play again and continue if yes and stop if no. Once the user stops playing the program should print the total number of wins.
I am having issues with declaring my variables in the correct places since I am trying to use a method so I can call it to play the game again.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
String answer = input.next();
}
public static int letsPlay()
{
int cMove;
int userMove = 0;
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Random r = new Random();
while (answer.equalsIgnoreCase("yes"))
cMove = r.nextInt(3)+1;
System.out.println("Choose your move!");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
while(input.hasNextInt()) {
if (userMove!=1 && userMove!=2 && userMove!=3)
{
System.out.println("Invalid move. Try again.");
System.out.println("Enter your move: ");
input.nextInt();
}
}
if(userMove==1)
{
System.out.println("You have chosen Rock!");
}
else if(userMove==2)
{
System.out.println("You have chosen Paper!");
}
else if(userMove==3)
{
System.out.println("You have chosen Scissors!");
}
if (userMove == cMove)
{
System.out.println("Tie Game!");
System.out.println("");
tie++;
rounds++;
} else if (cMove==1 && userMove==3)
{
System.out.println("Computer chose Rock!");
System.out.println("Rock beats Scissors!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==1 && userMove==2)
{
System.out.println("Computer chose Rock!");
System.out.println("Paper beats Rock!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==3)
{
System.out.println("Computer chose Paper!");
System.out.println("Scissors beats Paper!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==1)
{
System.out.println("Computer chose Paper!");
System.out.println("Paper beats Rock!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==3 && userMove==1)
{
System.out.println("Computer chose Scissors!");
System.out.println("Rock beats Scissors!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==3 && userMove==2)
{
System.out.println("Computer chose Scissors!");
System.out.println("Scissors beats Paper!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
System.out.println("Would you like to play again?");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
String yesorno = input.next();
if(yesorno.equalsIgnoreCase("yes"))
{
letsPlay();
}
else {
System.out.println ("Here are the final scores after " + rounds +" rounds:");
System.out.println ("You: "+ pScore + "Computer: "+ cScore + "Ties: " + tie);
}
}
}
Edited code so far, it says missing return statement from my letsPlay method:
Not sure how to proceed..
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
String answer = input.next();
letsPlay(answer);
}
public static int letsPlay(String answer)
{
int cMove;
int userMove = 0;
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Random r = new Random();
Scanner input = new Scanner(System.in);
cMove = r.nextInt(3)+1;
while (answer.equalsIgnoreCase("yes"))
System.out.println("Choose your move!");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
while(input.hasNextInt()) {
if (userMove!=1 && userMove!=2 && userMove!=3)
{
System.out.println("Invalid move. Try again.");
System.out.println("Enter your move: ");
input.nextInt();
}
}
if(userMove==1)
{
System.out.println("You have chosen Rock!");
}
else if(userMove==2)
{
System.out.println("You have chosen Paper!");
}
else if(userMove==3)
{
System.out.println("You have chosen Scissors!");
}
if (userMove == cMove)
{
System.out.println("Tie Game!");
System.out.println("");
tie++;
rounds++;
} else if (cMove==1 && userMove==3)
{
System.out.println("Computer chose Rock!");
System.out.println("Rock beats Scissors!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==1 && userMove==2)
{
System.out.println("Computer chose Rock!");
System.out.println("Paper beats Rock!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==3)
{
System.out.println("Computer chose Paper!");
System.out.println("Scissors beats Paper!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==1)
{
System.out.println("Computer chose Paper!");
System.out.println("Paper beats Rock!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==3 && userMove==1)
{
System.out.println("Computer chose Scissors!");
System.out.println("Rock beats Scissors!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==3 && userMove==2)
{
System.out.println("Computer chose Scissors!");
System.out.println("Scissors beats Paper!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
System.out.println("Would you like to play again?");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
answer = input.next();
if(answer.equalsIgnoreCase("yes"))
{
main(null);
}
else {
System.out.println ("Here are the final scores after " + rounds +"
rounds:");
System.out.println ("You: "+ pScore + "Computer: "+ cScore + "Ties: "
+ tie);
}
}
}
You aren't passing the String answer to your letsPlay() method and that's because your letsPlay() method can't take a String as a parameter because it is defined without parameters being passed. A solution to this problem is to change the method definition to require a String variable.
public static int letsPlay()
turns into
public static int letsPlay(String userInput)
then inside your method you use the variable userInput instead of String answer in the letsPLay(String userInput) method.
The next issue you run into is you're calling the method again within the method. This is called recursion and it's perfectly legal, however it is not ideal in this circumstance. You should exit the game once it's over and ask the user in your main() method if they'd like to play again.
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
do
{
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
String answer = input.nextLine();
letsPlay(answer);
}while(answer.equalsIgnoreCase("yes"));
}
Firstly, in your main method one input.next() is extra and of no use so remove it.
Now write a statement in main method as follows after String answer = input.next();
:
letsPlay(answer);
Put a parameter in letsPlay() method as follows:
public static void letsPlay(String answer) {
//Your code..........
//Some last edits...
Scanner input = new Scanner(System.in);
answer = input.next();
if(!(answer.equalsIgnoreCase("yes")))
{
System.out.println ("Here are the final scores after "+rounds+"
rounds:");
System.out.println("You:"+pScore+"Computer: "+cScore+"Ties: "+tie);
}
}
No required extra method for calling any line.
You can call main
Move codes to main from letsPlay method.
remove: letsPlay()
use: main(null)
import java.util.Scanner;
import java.util.Random;
public class Main
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
String answer = input.next();
// moved codes to following place from letsPlay
int cMove = 0;
...
if(yesorno.equalsIgnoreCase("yes"))
{
main(null); // changed with letsPlay()
}
...
}
}
cMove not initilazed exception occurred. So use this:
int cMove = 0;
Now, any errors not occurred.
Move the Scanner object inside the constructor. Currently you have it outside and your code does not know what it is.
Here is the modified portion of your code:
public static int letsPlay()
{
int cMove;
int userMove = 0;
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Random r = new Random();
Scanner input = new Scanner(System.in); // move the input object inside the constructor
Hope it helps.
There are many errors in your code. You can check out the comments in the code for a proper understanding of the code.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
// initialising variable to 0 for score calculation
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"Yes\" or \"No\"");
String answer = input.next();
if (answer.equalsIgnoreCase("yes")) {
// Calling method letsPlay with arguments answer, cScore, pScore, tie, rounds
// initially cScore = pScore = tie = rounds = 0
letsPlay(answer, cScore, pScore, tie, rounds);
}
}
// letsPlay Method
public static void letsPlay(String answer, int cScore, int pScore, int tie, int rounds) {
int cMove;
int userMove;
Random r = new Random();
Scanner input = new Scanner(System.in);
// loop untill user chose no
while (true) {
// to get random move of computer on every iteration
cMove = r.nextInt(3) + 1;
System.out.println("--------------------------------------------------");
System.out.println("Choose your move!");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
// loop until user input number 1 or 2 or 3
while (userMove != 1 && userMove != 2 && userMove != 3) {
System.out.println("Invalid move. Try again.");
System.out.println("--------------------------------------------------");
System.out.println("Choose your move: ");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
}
// Print statement for user move
if (userMove == 1) {
System.out.println("You have chosen Rock!");
} else if (userMove == 2) {
System.out.println("You have chosen Paper!");
} else {
System.out.println("You have chosen Scissors!");
}
// Print statement for computer move
if (cMove == 1) {
System.out.println("Computer chose Rock!");
} else if (cMove == 2) {
System.out.println("Computer chose Paper!");
} else {
System.out.println("Computer chose Scissors!");
}
// Winning, Loosing and Tie conditions
// increment round to 1 every time
// increment the winner, looser or tie on every iteration
if (userMove == cMove) {
System.out.println("Tie Game!");
tie++;
rounds++;
} else if (cMove == 1 && userMove == 3) {
System.out.println("Rock beats Scissors!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
} else if (cMove == 1 && userMove == 2) {
System.out.println("Paper beats Rock!");
System.out.println("Player Wins!");
pScore++;
rounds++;
} else if (cMove == 2 && userMove == 3) {
System.out.println("Scissors beats Paper!");
System.out.println("Player Wins!");
pScore++;
rounds++;
} else if (cMove == 2 && userMove == 1) {
System.out.println("Paper beats Rock!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
} else if (cMove == 3 && userMove == 1) {
System.out.println("Rock beats Scissors!");
System.out.println("Player Wins!");
pScore++;
rounds++;
} else if (cMove == 3 && userMove == 2) {
System.out.println("Scissors beats Paper!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
// Asking again to play or not
System.out.println("\nWould you like to play again?");
System.out.println("Answer \"Yes\" or \"No\"");
answer = input.next();
if (answer.equalsIgnoreCase("yes")) {
// If yes the call letsPlay(answer, cScore, pScore, tie, rounds);
// But this time value of cScore, pScore, tie, rounds is changed
// according to conditions
letsPlay(answer, cScore, pScore, tie, rounds);
} else {
// Print if user says didn't want to play again
System.out.println("==========================================");
System.out.println("\nHere are the final scores after " + rounds + " rounds:");
System.out.println("You : " + pScore + "\nComputer : " + cScore + "\nTies : " + tie);
}
// Exit if user didn't want to play again
break;
}
}
}

Rock Paper Scissors Game (Menu Method)

So as it says in the title, I am trying to create a RPS Game with a Menu as a Method, the thing is I don't know how to call inputs from that menu at any point.
For a better idea, this is my code:
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
String player1choice, player1Name;
int mainMenu,subMenu;
String again;
player1Name = "";
welcomeBanner ();
mainMenu = getMenu (keyboard);
if (mainMenu == 1)
{
keyboard.nextLine();
player1Name = getAName (keyboard);
for (int i = 0; i < 50; ++i) System.out.println();
main (null);
}
if (mainMenu == 2)
{
System.out.println("Welcome "+player1Name); //add name input
subMenu =getsubMenu (keyboard);
System.out.println("You have chosen: "); //add option chosen
System.out.println("Cpu has got, It's a Tie!");//cpuChoice add
}
if (mainMenu == 3)
{
keyboard.nextLine();
String exitRequest;
System.out.print("Are you sure you want to exit? (Y/N): ");
exitRequest = keyboard.nextLine ();
if (exitRequest.equals("y") || exitRequest.equals("Y"))
{
System.out.println("Good Bye!");
System.exit(0);
}
else if (exitRequest.equals("n") || exitRequest.equals("N"))
{
for (int i = 0; i < 50; ++i) System.out.println();
main(null);
}
}
}
static void welcomeBanner()
{
for (int i = 0; i < 60; i++)
{
System.out.print('*');
}
System.out.println("");
System.out.println("* Welcome To The Rock, Paper, Scissors Game *");
System.out.println("*----------------------------------------------------------*");
System.out.println("* Created by: Jonathan Gutierrez, and I am NoxBot! *");
for (int i = 0; i < 60; i++)
{
System.out.print('*');
}
System.out.println("");
System.out.println("");
}
static int getMenu (Scanner aKeyboard)
{
int playermenuChoice;
System.out.println("1. Enter Player Name");
System.out.println("2. Play a Game");
System.out.println("3. Exit Application");
System.out.println("");
System.out.print("Enter your choice: ");
playermenuChoice = aKeyboard.nextInt();
return playermenuChoice;
}
static int getsubMenu (Scanner aKeyboard)
{
int submenuChoice;
System.out.println("Enter 1 for Rock");
System.out.println("Enter 2 for Paper");
System.out.println("Enter 3 for Scissors");
System.out.println("");
System.out.print("Enter choice: ");
submenuChoice = aKeyboard.nextInt();
return submenuChoice;
}
static String getAName (Scanner aKeyboard)
{
String player1Info;
System.out.print("Enter your name: ");
player1Info = aKeyboard.nextLine ();
return player1Info;
}
static String computerChoice ()
{
String cpuChoice;
cpuChoice = "";
Random randomNumbers = new Random();
int cpu = randomNumbers.nextInt (2) + 1;
switch (cpu)
{
case 1:
cpuChoice = "Rock";
break;
case 2:
cpuChoice = "Paper";
break;
case 3:
cpuChoice = "Scissors";
break;
}
return cpuChoice;
}
So when the player chooses option 1, program asks to enter the name of the player, and i want to use that input at any point (most specifically when mainMenu ==2). How can i do that?
EDIT: this is my new code:
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
String player1choice, player1Name, subMenu;
int mainMenu;
String again;
player1Name = "";
welcomeBanner ();
mainMenu = getMenu (keyboard);
if (mainMenu == 1)
{
keyboard.nextLine();
player1Name = getAName (keyboard);
for (int i = 0; i < 50; ++i) System.out.println();
welcomeBanner ();
mainMenu = getMenu (keyboard);
System.out.println("");
System.out.println("Welcome " + player1Name);
System.out.println("");
}
if (mainMenu == 2)
{
subMenu =enterPlayersChoice (keyboard);
keyboard.nextLine();
String cmpu = computerChoice ();
for(int i = 0; i < 3; i ++)
if (subMenu.equals(cmpu))
System.out.println("It's a tie!");
else if (subMenu.equals("rock"))
if (cmpu.equals("scissors"))
System.out.println("Rock crushes scissors. You win!!");
else if (cmpu.equals("paper"))
System.out.println("Paper eats rock. You lose!!");
else if (subMenu.equals("paper"))
if (cmpu.equals("scissors"))
System.out.println("Scissor cuts paper. You lose!!");
else if (cmpu.equals("rock"))
System.out.println("Paper eats rock. You win!!");
else if (subMenu.equals("scissors"))
if (cmpu.equals("paper"))
System.out.println("Scissor cuts paper. You win!!");
else if (cmpu.equals("rock"))
System.out.println("Rock breaks scissors. You lose!!");
else System.out.println("Invalid user input.");
System.out.println("");
}
if (mainMenu == 3)
{
keyboard.nextLine();
String exitRequest;
System.out.print("Are you sure you want to exit? (Y/N): ");
exitRequest = keyboard.nextLine ();
if (exitRequest.equals("y") || exitRequest.equals("Y"))
{
System.out.println("Good Bye!");
System.exit(0);
}
else if (exitRequest.equals("n") || exitRequest.equals("N"))
{
for (int i = 0; i < 50; ++i) System.out.println();
main(null);
}
}
}
static void welcomeBanner()
{
for (int i = 0; i < 60; i++)
{
System.out.print('*');
}
System.out.println("");
System.out.println("* Welcome To The Rock, Paper, Scissors Game *");
System.out.println("*----------------------------------------------------------*");
System.out.println("* Created by: Jonathan Gutierrez, and I am NoxBot! *");
for (int i = 0; i < 60; i++)
{
System.out.print('*');
}
System.out.println("");
System.out.println("");
}
static int getMenu (Scanner aKeyboard)
{
int playermenuChoice;
System.out.println("1. Enter Player Name");
System.out.println("2. Play a Game");
System.out.println("3. Exit Application");
System.out.println("");
System.out.print("Enter your choice: ");
playermenuChoice = aKeyboard.nextInt();
return playermenuChoice;
}
public static String enterPlayersChoice(Scanner aKeyboard)
{
String input = "";
System.out.print("You have a choice of picking rock, paper, or scissors: ");
input = aKeyboard.nextLine();
String inputLower = input.toLowerCase();
return inputLower;
}
static String getAName (Scanner aKeyboard)
{
String player1Info;
System.out.print("Enter your name: ");
player1Info = aKeyboard.nextLine ();
return player1Info;
}
public static String computerChoice ()
{
String cpuChoice;
cpuChoice = "nothing";
Random randomNumbers = new Random();
int cpu = randomNumbers.nextInt (2) + 1;
switch (cpu)
{
case 1:
cpuChoice = "rock";
break;
case 2:
cpuChoice = "paper";
break;
case 3:
cpuChoice = "scissors";
break;
}
return cpuChoice;
}
}
To finish this i want the game to display a message whether player wins or lose, but it is being skipped (mainMenu ==2) any ideas?
Here's a way of rearranging your existing application in a different manner. Some of the major changes include making all methods non-static except the main method, and creating a RockPaperScissorsNew object for the application's point of entry. I've also added class variables so you don't need to pass your Scanner around as an object to all of your methods.
To answer your original question of how you're able to re-use the input entered by the user, the solution I provided is to retain that information within the class variable.
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissorsNew {
//Class variables
Scanner keyboard;
String player1choice, player1Name; //Name will be stored here.
int mainMenu,subMenu;
public RockPaperScissorsNew()
{
keyboard = new Scanner(System.in);
welcomeBanner(); //Display the welcome banner once.
while(true) //Repeatedly display the main menu.
getChoice(); //Get the user's choice
}
public void getChoice()
{
int choice = -1; //Set choice to fail first.
while (choice > 3 || choice < 0) //Wait until user choice passes.
{
choice = getMenu();
}
if (choice == 1) { //Choose your sub option.
getAName(); // Get the user name.
System.out.println("Your name is " + player1Name); //Saved
}
if (choice == 2)
getsubMenu();
if (choice == 3)
System.exit(0);
}
public void welcomeBanner()
{
for (int i = 0; i < 60; i++)
{
System.out.print('*');
}
System.out.println("");
System.out.println("* Welcome To The Rock, Paper, Scissors Game *");
System.out.println("*----------------------------------------------------------*");
System.out.println("* Created by: Jonathan Gutierrez, and I am NoxBot! *");
for (int i = 0; i < 60; i++)
{
System.out.print('*');
}
System.out.println("");
System.out.println("");
}
public int getMenu ()
{
int playermenuChoice;
System.out.println("1. Enter Player Name");
System.out.println("2. Play a Game");
System.out.println("3. Exit Application");
System.out.println("");
System.out.print("Enter your choice: ");
playermenuChoice = Integer.parseInt(keyboard.nextLine().trim());
return playermenuChoice;
}
public int getsubMenu ()
{
int submenuChoice;
System.out.println("Enter 1 for Rock");
System.out.println("Enter 2 for Paper");
System.out.println("Enter 3 for Scissors");
System.out.println("");
System.out.print("Enter choice: ");
submenuChoice = Integer.parseInt(keyboard.nextLine().trim());
return submenuChoice;
}
//This method has been changed to use the class variable, and no longer
//returns a string.
public void getAName ()
{
//String player1Info;
System.out.print("Enter your name: ");
player1Name = keyboard.nextLine ();
//return player1Info;
}
public String computerChoice ()
{
String cpuChoice;
cpuChoice = "";
Random randomNumbers = new Random();
int cpu = randomNumbers.nextInt (2) + 1;
switch (cpu)
{
case 1:
cpuChoice = "Rock";
break;
case 2:
cpuChoice = "Paper";
break;
case 3:
cpuChoice = "Scissors";
break;
}
return cpuChoice;
}
public static void main(String...args)
{
new RockPaperScissorsNew();
}
}

Returning to the start of a loop in Java?

I have to create a basic three-round rock paper scissors game for my beginners Java class. I've gotten everything in my code to work except my "Play again?" command. I know I'm supposed to use a loop for this, but I'm not entirely sure where in the code to insert the loop. Here's the code I currently have:
import java.util.Scanner;
import java.util.Random;
public class RPSGameFinal {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Rock Paper Scissors! Best two out of three!");
System.out.println("Please enter \"Rock\", \"Paper\", or \"Scissors\".");
int playerWins = 0;
int computerWins = 0;
int roundNumber = 0;
if (roundNumber < 3)
{
while (roundNumber != 2)
{
Scanner keyboard = new Scanner(System.in);
String playerChoice = keyboard.next();
if (playerChoice.equalsIgnoreCase("Rock"))
{
roundNumber = roundNumber + 1;
Random computerChoice = new Random();
int choiceValue = computerChoice.nextInt(3) + 1;
if (choiceValue == 1)
{
System.out.println("Rock vs Rock, Tie!");
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
else if (choiceValue == 2)
{
System.out.println("Rock vs Paper, Computer Wins!");
computerWins = computerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
else if (choiceValue == 3)
{
System.out.println("Rock vs Scissors, Player wins!");
playerWins = playerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
}
else if (playerChoice.equalsIgnoreCase("Paper"))
{
roundNumber = roundNumber + 1;
Random computerChoice = new Random();
int choiceValue = computerChoice.nextInt(3) + 1;
if (choiceValue == 1)
{
System.out.println("Paper vs Rock, Player wins!");
playerWins = playerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
else if (choiceValue == 2)
{
System.out.println("Paper vs Paper, Tie!");
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
else if (choiceValue == 3)
{
System.out.println("Paper vs Scissors, Computer wins!");
computerWins = computerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
}
else if (playerChoice.equalsIgnoreCase("Scissors"))
{
roundNumber = roundNumber + 1;
Random computerChoice = new Random();
int choiceValue = computerChoice.nextInt(3) + 1;
if (choiceValue == 1)
{
System.out.println("Scissors vs Rock, Computer wins!");
computerWins = computerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
else if (choiceValue == 2)
{
System.out.println("Scissors vs Paper, Player wins!");
playerWins = playerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
else if (choiceValue == 3)
{
System.out.println("Scissors vs Scissors, Tie!");
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\".");
}
}
}
if (roundNumber == 2);
{
Scanner keyboard2 = new Scanner(System.in);
String input2 = keyboard2.next();
if (input2.equalsIgnoreCase("Rock"))
{
Random computerChoice2 = new Random();
int choiceValue2 = computerChoice2.nextInt(3) + 1;
if (choiceValue2 == 1)
{
System.out.println("Rock vs Rock, Tie!");
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
else if (choiceValue2 == 2)
{
System.out.println("Rock vs Paper, Computer wins!");
computerWins = computerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
else if (choiceValue2 == 3)
{
System.out.println("Rock vs Scissors, Player wins!");
playerWins = playerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
}
else if (input2.equalsIgnoreCase("Paper"))
{
Random computerChoice2 = new Random();
int choiceValue2 = computerChoice2.nextInt(3) + 1;
if (choiceValue2 == 1)
{
System.out.println("Paper vs Rock, Player wins!");
playerWins = playerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
else if (choiceValue2 == 2)
{
System.out.println("Paper vs Paper, Tie!");
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
else if (choiceValue2 == 3)
{
System.out.println("Paper vs Scissors, Computer wins!");
computerWins = computerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
}
else if (input2.equalsIgnoreCase("Scissors"))
{
Random computerChoice2 = new Random();
int choiceValue2 = computerChoice2.nextInt(3) + 1;
if (choiceValue2 == 1)
{
System.out.println("Scissors vs Rock, Computer wins!");
computerWins = computerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
else if (choiceValue2 == 2)
{
System.out.println("Scissors vs Paper, Player wins!");
playerWins = playerWins + 1;
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again?");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
else if (choiceValue2 == 3)
{
System.out.println("Scissors vs Scissors, Tie!");
System.out.println("Player has won "+playerWins+" times and the computer has won "+computerWins+" times.");
if (playerWins > computerWins)
{
System.out.println("Player wins!");
System.out.println("Play again? Enter Yes or No.");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins < computerWins)
{
System.out.println("Computer wins!");
System.out.println("Play again? Enter Yes or No.");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
else if (playerWins == computerWins)
{
System.out.println("Tie game!");
System.out.println("Play again? Enter Yes or No.");
Scanner keyboard3 = new Scanner(System.in);
String input3 = keyboard3.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
System.out.println("Thank you for playing!");
}
}
}
}
}
}
}
}
Which type of loop should I use and where should it be placed? Many thanks in advance!
Use a while loop right after your main method.
int playAgain = 1;
while(playAgain == 1){
//your code
System.out.println("Would you like to play again? Enter 1 for yes, 2 for no");
//Get User input here and set it to the variable playAgain
}
}//end main
}//end class
checkout the documentation for the "continue" keyword, specifically using "labels".
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
do a "find in page" for "ContinueWithLabelDemo".
Basically, you can label a line to continue execution.
The problem is that the code is very long and very difficult to maintain ... a quick fix will be wrap all in a function ( change 'main' to 'game' ) and then add a boolean to the main. this boolean will decide if to run another game :
public static void game() { /*all code of main goes here*/ }
public static void main(String[] args) {
boolean quitGame=false;
while(!quitGame){
game(); // calling to start a game
System.out.println("Play again? Enter Yes or No.");
Scanner keyboard = new Scanner(System.in);
String input3 = keyboard.next();
if (input3.equalsIgnoreCase("Yes"))
{
}
else
{
quitGame = true; // this will exit the loop
}
}
}
Answer:
I restructured your code in the hope that you will better understand why I put my loops where I did:
public static void main( String[] args )
{
Scanner scanner = new Scanner( System.in );
Random random = new Random();
System.out.println( "Welcome to Rock Paper Scissors! Best two out of three!" );
boolean playAgain = false;
do
{
int playerWins = 0;
int computerWins = 0;
for ( int i = 1; i < 4; i++ )
{
System.out.println( "Roundnumber: " + i );
String playerChoice = inputPlayerChoice( scanner );
int computerChoice = calculateComputerChoice( random );
RpsResult result = calculateResult( playerChoice, computerChoice );
boolean isPlayerWin = result.isPlayerWin();
boolean isTie = result.isTie();
if ( !isTie )
{
if ( isPlayerWin )
{
playerWins++;
}
else
{
computerWins++;
}
}
System.out.println( "Player has won " + playerWins + " times and the computer has won " + computerWins + " times." );
}
showFinalResult( playerWins, computerWins );
System.out.println( "Play again?" );
playAgain = askIfPlayAgain( scanner );
}
while ( playAgain );
System.out.println( "Thank you for playing!" );
scanner.close();
}
If you take a look at the main method you will see, that I put a do-while loop around the mainpart of the program. This is for repeating the whole game again. I choose a do-while loop because you want to run the game at least once.
Since you always want to play exact 3 rounds I also added a for-loop around the inner section. This has the benefit, that you don't need to write your code 3 times - since in all 3 rounds actually happens the same (userInput, calculate computer choice, see if the user or the computer wins this round, write the result of the round on the console)
After the for-loop you show the final result, ask the player if he wants to repeat the game and set the playAgain variable that will break the loop if its false.
For your information:
This is how I broke your code down to fit the showed main method.
These are the methods to get the player choice and the computer choice:
private static String inputPlayerChoice( Scanner scanner )
{
System.out.println( "Please enter \"Rock\", \"Paper\", or \"Scissors\"." );
String playerChoice = scanner.next();
return playerChoice;
}
private static int calculateComputerChoice( Random random )
{
int computerChoice = random.nextInt( 3 ) + 1;
return computerChoice;
}
This is how the result of a round is calculated - sincs its always the same I moved it to its own method:
private static RpsResult calculateResult( String playerInput, int computerChoice )
{
boolean playerWin = false;
boolean tie = false;
if ( playerInput.equalsIgnoreCase( "Rock" ) )
{
switch ( computerChoice )
{
case 1:
System.out.println( "Rock vs Rock, Tie!" );
tie = true;
break;
case 2:
System.out.println( "Rock vs Paper, Computer Wins!" );
break;
case 3:
System.out.println( "Rock vs Scissors, Player wins!" );
playerWin = true;
break;
}
}
else if ( playerInput.equalsIgnoreCase( "Paper" ) )
{
switch ( computerChoice )
{
case 1:
System.out.println( "Paper vs Rock, Player wins!" );
playerWin = true;
break;
case 2:
System.out.println( "Paper vs Paper, Tie!" );
tie = true;
break;
case 3:
System.out.println( "Paper vs Scissors, Computer wins!" );
break;
}
}
else if ( playerInput.equalsIgnoreCase( "Scissors" ) )
{
switch ( computerChoice )
{
case 1:
System.out.println( "Scissors vs Rock, Computer wins!" );
break;
case 2:
System.out.println( "Scissors vs Paper, Player wins!" );
playerWin = true;
break;
case 3:
System.out.println( "Scissors vs Scissors, Tie!" );
tie = true;
break;
}
}
RpsResult result = new RpsResult();
result.setPlayerWin( playerWin );
result.setTie( tie );
return result;
}
RpsResult is a simple storage Object:
public class RpsResult
{
private boolean playerWin;
private boolean tie;
/**
* #return the playerWin
*/
public boolean isPlayerWin()
{
return playerWin;
}
/**
* #param playerWin
* the playerWin to set
*/
public void setPlayerWin( boolean playerWin )
{
this.playerWin = playerWin;
}
/**
* #return the tie
*/
public boolean isTie()
{
return tie;
}
/**
* #param tie
* the tie to set
*/
public void setTie( boolean tie )
{
this.tie = tie;
}
}
To print the result message I used this method:
private static void showFinalResult( int playerWins, int computerWins )
{
System.out.println( "Final result: playerWins " + playerWins + " computerWins " + computerWins );
if ( playerWins > computerWins )
{
System.out.println( "Player has won" );
}
else
{
System.out.println( "Computer has won" );
}
}
And the question if the player wants to play again:
private static boolean askIfPlayAgain( Scanner scanner )
{
String input = scanner.next();
if ( input.equalsIgnoreCase( "Yes" ) )
{
return true;
}
else
{
return false;
}
}
boolean playing = true;
while(playing){
//your game code
System.out.println("Would you like to play again?");
//get user input
if(user said no){
playing = false;
}
This is a basic outline one how to keep a continuous loop to execute through your program
You could use a loop to loop however many times you would like. There are three different kinds that would work here: a for loop, a while loop, or a do while. However, I would suggest using a for loop and just using a boolean operator in the method to decide when you would like for it to stop looping

Rock paper scissors, play again?

For my assignment I'm supposed make the program rock paper scissors which I figured that out my main problem is I can't get the program to play again right or get the program to calculate the game scores correctly pleas help I'm going crazy trying to figure this out?!
//Tayler Dorsey
import java.util.Random;
import java.util.Scanner;
public class PRS {
static Scanner keyboard = new Scanner(System. in );
public static void instructions() {
System.out.println("This is the popular game of paper, rock, scissors. Enter your\nchoice by typing the word \"paper\", the word \"rock\" or the word\n\"scissors\". The computer will also make a choice from the three\noptions. After you have entered your choice, the winner of the\ngame will be determined according to the following rules:");
System.out.println("Paper wraps rock (paper wins)\nRock breaks scissors (rock wins)\nScissors cuts paper (scissors wins)");
System.out.println("If both you and the computer enter the same choice, then the game is tied.");
}
public static int playGame() {
int ties = 0, wins = 0, losts = 0;
String userchoice, computerchoice;
System.out.println("Enter your choice: ");
userchoice = keyboard.next();
computerchoice = computerChoose();
System.out.println("You entered: " + userchoice);
System.out.println("Computer choose: " + computerchoice);
if ((userchoice.equals("paper") && computerchoice.equals("paper")) || (userchoice.equals("rock") && computerchoice.equals("rock")) || (userchoice.equals("scissors") && computerchoice.equals("scissors"))) {
System.out.println("IT'S A TIE!");
ties++;
return 3;
} else if ((userchoice.equals("paper") && computerchoice.equals("rock")) || (userchoice.equals("rock") && computerchoice.equals("scissors")) || (userchoice.equals("scissors") && computerchoice.equals("paper"))) {
System.out.println("YOU WIN!");
wins++;
return 1;
} else {
System.out.println("YOU LOSE!");
losts++;
return 2;
}
}
public static String computerChoose() {
Random generator = new Random();
String[] answer = new String[3];
answer[0] = "paper";
answer[1] = "rock";
answer[2] = "scissors";
return answer[generator.nextInt(3)];
}
public static void main(String[] args) {
String play;
Scanner keyboard = new Scanner(System. in );
System.out.println("The Game of Paper, Rock, Scissors");
System.out.println("Do you need instructions (y or n)?");
String help = keyboard.nextLine();
if (help.equals("y")) instructions();
int result = playGame();
System.out.println("Play again (y or n)?");
play = keyboard.nextLine();
if (play.equals("y"));
else {
int count = 0, wins = 0, losts = 0, ties = 0;
System.out.println("Games played: " + count);
System.out.println("Wins for you: " + wins);
System.out.println("Wins for me: " + losts);
System.out.println("Tied games: " + ties);
}
do {} while (play == "y"); {
playGame();
int count = 0;
count++;
}
}
}
There are two issues here:
The code isn't inside the do-while loop, it's after it.
String equality should be checked with equals not with ==.
So:
int count = 0;
do { // Note the code inside the do-while block
playGame();
count++;
} while (play.equals("y")); // Note the use of equals
Do the following:
do {
play = keyboard.nextLine();
if (play.equals("y")){
}
else {
int count = 0, wins = 0, losts = 0, ties = 0;
System.out.println("Games played: " + count);
System.out.println("Wins for you: " + wins);
System.out.println("Wins for me: " + losts);
System.out.println("Tied games: " + ties);
}
} while (play.equals("y"));
In order to calculate the games scores make those variable global.
public class PRS {
static Scanner keyboard = new Scanner(System. in );
int ties = 0, wins = 0, losts = 0;

Categories

Resources