I have an assignment that I have been working on that is to modify an existing rock paper scissors program so that I ahve a method that sets the player name and a method that gets the player's name. I have created the methods however it does not allow me to input the player name it skips to where it asks for the amount of rounds.
The output dialogue looks like this;
"What is your name? How many rounds? 3
Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): 1
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]throw ROCK.
Computer throws SCISSORS.
You win!
Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): 2
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]throw PAPER.
Computer throws ROCK.
You win!
Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): 3
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]throw SCISSORS.
Computer throws SCISSORS.
It's a draw!
You win!"
This is the code that I am using;
/*
* RPS2.java from Chapter 8
* An object-oriented version of Rock Paper Scissors
* Lawrenceville Press
* June 10, 2005
*/
import java.util.Scanner;
/**
* Computer plays Rock Paper Scissors against one player.
*/
rounds = input.nextInt();
for (int i = 0; i < rounds; i++) {
System.out.print("Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): ");
playerThrow = input.nextInt();
rpsOpponent.makeThrow(playerThrow);
rps.makeCompThrow();
rps.announceWinner(rpsOpponent.getThrow());
}
rps.bigWinner();
}
static String name;
public static String assignName() {
System.out.print("What is your name? ");
Scanner nameinput = new Scanner(System.in);
name = nameinput.toString();
return RPS2.name;
}
public String getName(){
return RPS2.name;
}
}
/**
* models the player in a game of RPS
*/
public class RPSPlayer {
private int playerThrow; //ROCK = 1, PAPER = 2, SCISSORS = 3
/**
* constructor
* pre: none
* post: RPSPlayer object created. The player is given a default throw.
*/
public RPSPlayer() {
playerThrow = 1; //default throw
}
/**
* Sets the player's throw.
* pre: newThrow is the integer 1, 2, or 3.
* post: Player's throw has been made.
*/
public void makeThrow(int newThrow){
playerThrow = newThrow;
}
/**
* Returns the player's throw.
* pre: none
* post: Player's throw has been returned.
*/
public int getThrow() {
return(playerThrow);
}
}
/**
* Models a game of RPS
*/
import java.util.Random;
public class RPSGame {
public static final int ROCK = 1, PAPER = 2, SCISSORS = 3;
private int compThrow;
private int playerWins = 0, computerWins = 0;
/**
* constructor
* pre: none
* post: RPSGame object created. Computer throw generated.
*/
public RPSGame() {
Random rand = new Random();
compThrow = rand.nextInt(3) + 1; //random integer between 1 and 3
playerWins = 0;
computerWins = 0;
}
/**
* Computer's throw is generated (1 for ROCK, 2 for PAPER, 3 for SCISSORS).
* pre: none
* post: Computer's throw generated.
*/
public void makeCompThrow(){
Random rand = new Random();
compThrow = rand.nextInt(3) + 1; //random integer between 1 and 3
}
/**
* Returns the computer's throw.
* pre: none
* post: Computer's throw has been returned.
*/
public int getCompThrow() {
return(compThrow);
}
/**
* Determines the winner of the round.
* pre: playerThrow is the integer 1, 2, or 3.
* post: Displays a message indicating throws. Compares player's throw
* to computer's throw and displays a message indicating the winner.
*/
public void announceWinner(int playerThrow) {
RPS2 rps2= new RPS2();
System.out.print(rps2.getName() + " throws ");
switch (playerThrow) {
case ROCK: System.out.println("ROCK."); break;
case PAPER: System.out.println("PAPER."); break;
case SCISSORS: System.out.println("SCISSORS."); break;
}
System.out.print("Computer throws ");
switch (compThrow) {
case ROCK: System.out.println("ROCK."); break;
case PAPER: System.out.println("PAPER."); break;
case SCISSORS: System.out.println("SCISSORS."); break;
}
/* Determine and annouce winner */
if (playerThrow == ROCK && compThrow == ROCK) {
System.out.println("It's a draw!");
} else if (playerThrow == ROCK && compThrow == PAPER) {
System.out.println("Computer wins!");
computerWins += 1;
} else if (playerThrow == ROCK && compThrow == SCISSORS) {
System.out.println("You win!");
playerWins += 1;
}
if (playerThrow == PAPER && compThrow == ROCK) {
System.out.println("You win!");
playerWins += 1;
} else if (playerThrow == PAPER && compThrow == PAPER) {
System.out.println("It's a draw!");
} else if (playerThrow == PAPER && compThrow == SCISSORS) {
System.out.println("Computer wins!");
computerWins +=1;
}
if (playerThrow == SCISSORS && compThrow == ROCK) {
System.out.println("Computer wins!");
computerWins += 1;
} else if (playerThrow == SCISSORS && compThrow == PAPER) {
System.out.println("You win!");
playerWins += 1;
} else if (playerThrow == SCISSORS && compThrow == SCISSORS) {
System.out.println("It's a draw!");
}
}
/**
* Displays the overall winner.
* pre: none
* post: Computer and player wins compared and
* an overall winner announced.
*/
public void bigWinner() {
if (computerWins > playerWins){
System.out.println("Computer wins!");
} else if (playerWins > computerWins){
System.out.println("You win!");
} else {
System.out.println("It's a draw!");
}
}
}