Basically I need to setup a conditional loop to have the game run up to 10 times and then have it display the results for both the computer and user. I have my display function in the works but how could i setup the loop or counter to run the match 10 times? Any help would be appreciated. Below is my entire code thus far. i've trimmed out alot of the if/else statements for pure strings/functions.
// Create object for reading from the keyboard.
static Scanner input = new Scanner(System.in);
// Class Contastant to simplify comparisons.
static final String ROCK = "Rock";
static final String PAPER = "Paper";
static final String SCISSORS = "Scissors";
public static void main(String[] args) {
// Get choices
String computerChoice = getComputerChoice();
String humanChoice = getHumanChoice();
// Display the choices that were made
System.out.println("Computer Chose: " + computerChoice);
System.out.println("Human Chose: " + humanChoice);
// Computer the winner
String winner = computeWinner(humanChoice, computerChoice);
// Display the winner
System.out.println("The Winner is: " + winner);
}
// Calculate the winner from the computer and human choices.
private static String computeWinner(String humanChoice, String computerChoice) {
String winner;
// A chain of nested if/else statements. Notice that the tie is only
// checked once. (If the choices are the same, it doesn't matter what
// was specifically chosen!)
if (humanChoice.equalsIgnoreCase(computerChoice)) {
winner = "Game is a Tie!";
} else if (humanChoice.equalsIgnoreCase(ROCK)) {
if (computerChoice.equals(SCISSORS)) {
winner = "Human";
} else {
winner = "Computer";
}
} else if (humanChoice.equalsIgnoreCase(PAPER)) {
if (computerChoice.equals(ROCK)) {
winner = "Human";
} else {
winner = "Computer";
}
} else if (humanChoice.equalsIgnoreCase(SCISSORS)) {
if (computerChoice.equals(PAPER)) {
winner = "Human";
} else {
winner = "Computer";
}
} else {
winner = "There are no winners with invalid input!";
}
return winner;
}
// Get the computer's choice
private static String getComputerChoice() {
// Get a random number between 0 and 2
int randomValue = (int) (Math.random() * 3.0);
String computerChoice;
if (randomValue == 0) {
computerChoice = ROCK;
} else if (randomValue == 1) {
computerChoice = PAPER;
} else {
computerChoice = SCISSORS;
}
return computerChoice;
}
// Get the human's choice
private static String getHumanChoice() {
System.out.print("Please enter Rock, Scissor, or Paper: ");
String inString = input.nextLine();
if (inString.equalsIgnoreCase("rock") || inString.equalsIgnoreCase("paper") ||
inString.equalsIgnoreCase("scissor")){
return inString;
}
else{
System.out.print("Invalid entry, ");
return getHumanChoice();
}
}
}