java beginner rock paper scissors game - java

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;
}
}
}

Related

Rock Paper Scissors Difficulty Java

Im trying to make a rock, paper, scissors game where you choose 1,2 or 3 for rock then displays what you've chosen and what the computer has chosen. I then need to keep track of score for wins and at the end of every round ask the user if they want to play again.
import java.util.Scanner;
public class Rock5
{
//-----------------------------------------------------------------
// Plays the Rock-Paper-Scissors game with the user.
//-----------------------------------------------------------------
public static void main (String[] args)
{
final int OPTIONS = 3;
final int ROCK = 1, PAPER = 2, SCISSORS = 3;
final int COMPUTER = 1, PLAYER = 2, TIE = 3;
int computer, player, winner = 0;
int wins = 0, losses = 0, ties = 0;
String again;
Scanner in = new Scanner(System.in);
do
{
computer = (int) (Math.random() * OPTIONS) + 1;
System.out.println();
System.out.print ("Enter your choice - 1 for Rock, 2 for " +
"Paper, and 3 for Scissors: ");
player = in.nextInt();
System.out.print ("My choice was ");
// Determine the winner
switch (computer)
{
case ROCK:
System.out.println ("Rock.");
if (player == SCISSORS)
winner = COMPUTER;
else
if (player == PAPER)
winner = PLAYER;
else
winner = TIE;
break;
case PAPER:
System.out.println ("Paper.");
if (player == ROCK)
winner = COMPUTER;
else
if (player == SCISSORS)
winner = PLAYER;
else
winner = TIE;
break;
case SCISSORS:
System.out.println ("Scissors.");
if (player == PAPER)
winner = COMPUTER;
else
if (player == ROCK)
winner = PLAYER;
else
winner = TIE;
}
// Print results and increment appropriate counter
if (winner == COMPUTER)
{
System.out.println ("I win!");
losses++;
}
else
if (winner == PLAYER)
{
System.out.println ("You win!");
wins++;
}
else
{
System.out.println ("We tied!");
ties++;
}
System.out.println();
System.out.print ("Play again (y/n)?: ");
again = in.nextLine();
}
while (again.equalsIgnoreCase ("y"));
// Print final results
System.out.println();
System.out.println ("You won " + wins + " times.");
System.out.println ("You lost " + losses + " times.");
System.out.println ("We tied " + ties + " times.");
}
}
Use
player = Integer.valueOf(in.nextLine());
Instead of
player = in.nextInt();
and it'll work, because if not, the number you enter goes into player, and the \n characater goes into again = in.nextLine() and because it's different of y it stops.

How to restart Rock Paper and Scissors game mutliple times until user quits in Java

Hello I'm stuck on how to keep the game going until the user decides to quit. I want to try any way possible to get the game to end and display the amount of losses, ties, and wins. I'm trying to use a while loop.
import java.util.Scanner;
import java.util.Random;
public class JavaApplication15 {
public static void main(String[] args) {
int ties = 0, wins = 0, losses = 0;
String comp = "";
String user = "";
Random choice = new Random();
String startover = "y";
while (startover.equals("y")) {
int computerchoice = choice.nextInt(3) + 1;
if (computerchoice == 1)
comp = "R";
else if (computerchoice == 2)
comp = "P";
else if (computerchoice == 3)
comp = "S";
Scanner scanner = new Scanner(System.in);
System.out.println("Enter rock (1), paper (2), or scissors (3) [no to quit]: ");
int player = scanner.nextInt();
if (player == 1) {
user = "R";
} else if (player == 2) {
user = "P";
} else if (player == 3) {
user = "S";
}
System.out.println("You choose: " + user);
System.out.println("Computer chooses: " + comp);
if (comp.equals(user)) {
System.out.println("It's a tie!");
ties++;
} else if (user.equals("R")) {
if (comp.equals("P"))
System.out.println("Computer wins! Paper beats rock!");
losses++;
} else if (comp.equals("S")) {
System.out.println("You win! Your rock beats the scissors!");
wins++;
} else if (user.equals("S")) {
if (comp.equals("P"))
System.out.println("You win! Scissor beats paper!");
wins++;
} else if (comp.equals("R")) {
System.out.println("Computer wins! Rock beats paper!");
losses++;
} else if (user.equals("P"))
if (comp.equals("R")) {
System.out.println("You win! Paper beats rock!");
wins++;
} else if (comp.equals("S")) {
System.out.println("Computer wins! Scissors beats paper!");
losses++;
}
}
{
System.out.println("Invalid input! Try again");
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no";
}
}
instead of doing scanner.nextInt(); you could do
System.out.println("Enter rock (1), paper (2), or scissors (3) [\'n\' to quit]: ");
char selection = scanner.nextLine().charAt(0)
if(selection == '1'){ user = "R"; }
else if(selection == '2'){ user = "P"; }
else if(selection == '3'){ user = "S"; }
else if(selection == 'n'){
System.out.println(ties)
System.out.println(wins);
System.out.println(losses);
return;
}
this makes the selection variable a char, and you can test if it's 1, 2, or 3 (that is, the characters 1 2 or 3), and also check for 'n'.
since you're working in the main method, simply putting "return" will exit the program at that point.
You just need add a new option 4
else if (computerchoice == 4)
comp = "E";
} else if (comp.equals("E")) {
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no";
}
I tried out your code as-is, and what I found out is that your code infact never stops. The while loop has no proper condition to exit. So I made some minor changes to make it work.
int ties = 0, wins = 0, losses = 0;
String comp = "";
String user = "";
Random choice = new Random();
String startover = "y";
Scanner scanner = new Scanner(System.in); //Scanner object should not be open in loop, it is a resource wastage
while (startover.equals("y")) {
int computerchoice = choice.nextInt(3) + 1;
if (computerchoice == 1)
comp = "R";
else if (computerchoice == 2)
comp = "P";
else if (computerchoice == 3)
comp = "S";
System.out.println("Enter rock (1), paper (2), or scissors (3) [no to quit]: ");
String input = scanner.nextLine(); //take input in string
int player ;
try {
player = Integer.parseInt(input); //see if actually number was given as input
} catch(Exception ex) {
break; //if not, then we assume player wants to quit
}
if (player == 1) {
user = "R";
} else if (player == 2) {
user = "P";
} else if (player == 3) {
user = "S";
}
System.out.println("You choose: " + user);
System.out.println("Computer chooses: " + comp);
if (comp.equals(user)) {
System.out.println("It's a tie!");
ties++;
} else if (user.equals("R")) {
if (comp.equals("P"))
System.out.println("Computer wins! Paper beats rock!");
losses++;
} else if (comp.equals("S")) {
System.out.println("You win! Your rock beats the scissors!");
wins++;
} else if (user.equals("S")) {
if (comp.equals("P"))
System.out.println("You win! Scissor beats paper!");
wins++;
} else if (comp.equals("R")) {
System.out.println("Computer wins! Rock beats paper!");
losses++;
} else if (user.equals("P"))
if (comp.equals("R")) {
System.out.println("You win! Paper beats rock!");
wins++;
} else if (comp.equals("S")) {
System.out.println("Computer wins! Scissors beats paper!");
losses++;
}
}
scanner.close(); //scanner was not closed before
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no"; //redundant actually

Java Programming - Beginner for Paper Rocks Scissors

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);
}
}

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

Java rock paper scissors loop

I'm having to make a paper rock scissors program that has the user enter in a choice, then tests it against the computer's choice. After every game, it should ask the player if they want to continue, and they should enter in 'Y' or 'N' to continue or quit. The best I could think was a while loop, and everything works fine except the very last bit.
import java.util.Scanner;
public class rockpaperscissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char cont = 'y';
while (cont == 'y'){
int com = (int)(Math.random() * 3);
System.out.println("Paper (0), Rock (1), or Scizzors (2)?");
int hum = input.nextInt();
if (com==(hum))
System.out.println("It's a tie!");
else if (hum == 0)
{
if (com == 1)
System.out.println ("You chose paper, computer chose rock You Win!");
else if (com == 2)
System.out.println ("You chose paper, Computer chose scissors You Lose!");
}
else if (hum == 1)
{
if (com == 2)
System.out.println ("You chose Rock, computer chose scissors You Win!");
else if (com == 0)
System.out.println ("You chose Rock, Computer chose paper You Lose!");
}
else if (hum == 2)
{
if (com == 0)
System.out.println ("You chose Scissors, computer chose paper You Win!");
else if (com == 1)
System.out.println ("You chose Scissors, Computer chose rock You Lose!");
}
System.out.println("Would you like to continue? (Y/N)");
cont = input.nextLine().charAt(0);
}
}
}
When I run it, the loop runs fine, the game is played, but then I get a 'string out of index range' error. Any idea how to resolve this?
Your nextInt() just reads the number from the input buffer, leaving the new line in it. So when you call input.nextLine() you're getting an empty line - the rest of the first line after the number. You should read the next-line and make sure it's not empty. If it is, just read it again.
Incidentally, your code that figures out who won is a bit cumbersome. If I were you, I would try to make it a little more general and clean. Think about a solution that can handle a more complex game, such as Rock Paper Scissors Lizard Spock without adding too much code.
When you get the answer from the user, you don't read the next line so the scanner still has a new line character. Then when you read the nextline you read that new line, and therefore there is no charat(0).
Change:
cont = input.nextLine().charAt(0);
to:
cont = input.next().charAt(0);
package rockpaper;
import java.util.Scanner;
/**
*
* #author Allen E.
*/
public class RockPaper {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int rock = 0;
int paper = 1;
int Scissors = 2;
int user = 0;
int computer = 0;
int gamesplayed = 0;
Scanner scan = new Scanner(System.in);
while (gamesplayed < 3)
{
System.out.println("Rock = 0 , Paper = 1, Scissors = 2");
String userinput = scan.nextLine();
int convertinput = Integer.valueOf(userinput);
int Computerinput = (int)(Math.random()*3);
if (Computerinput == 1 && convertinput == 0)
{
System.out.println("Paper beats Rock " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 1 && Computerinput == 0)
{
System.out.println("Paper beats Rock " +
"\nYou Win!");
gamesplayed++;
user++;
}
if (Computerinput == 0 && convertinput == 2)
{
System.out.println("Rock beats Scissors " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 0 && Computerinput == 2)
{
System.out.println("Rock beats Scissors " +
"\nYou Win!");
gamesplayed++;
user++;
}
if (Computerinput == 2 && convertinput == 1)
{
System.out.println("Scissors beats Paper " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 2 && Computerinput == 1 )
{
System.out.println("Scissors beats Paper " +
"\nYou Win");
gamesplayed++;
user++;
}
/*************************************************
* *
* *
* Handling a tie *
* *
* *
*************************************************/
if (Computerinput == 0 && convertinput == 0)
{
System.out.println("Rock ties Rock " +
"\nTie");
}
if (Computerinput == 1 && convertinput == 1)
{
System.out.println("Paper ties Paper " +
"\nTie");
}
if (Computerinput == 2 && convertinput == 2)
{
System.out.println("Scissors ties Scissors " +
"\nTie");
}/*End of While Loop*/
}
}
}

Categories

Resources