Answered You can hire a professional tutor to get the answer.
Just recently wrote this code and I am looking to see how I can add an error message incase the input does not follow the instructions as well as a...
Just recently wrote this code and I am looking to see how I can add an error message incase the input does not follow the instructions as well as a way to have the players enter their name. here is a copy of the code below,
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
String playagain;
@SuppressWarnings("UnusedAssignment")
char playAgainOrNot;
playAgainOrNot = 0;
int user1Input, user2Input;
//Rock = 1
//Paper = 2
//Scissors = 3
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Instructions: Type 1 for Rock, 2 for Paper, or 3 for Scissors");
System.out.println("What you would like to play player 1?");
System.out.println("Rock, paper, or scissors ");
user1Input = keyboard.nextInt();
while(true);
System.out.println("What you would like to play player 2?");
System.out.println("Rock, paper, or scissors ");
user2Input = keyboard.nextInt();
//if the users choices are both equal
if (user1Input == user2Input)
{
System.out.println("It's a tie!");
}
else if (user1Input != user2Input)
{
//rock breaks scissors
if (user1Input == 1 && user2Input == 3)
{
System.out.println("Player 1 chose Rock");
System.out.println("Player 2 chose Scissors");
System.out.println("Rock Breaks Scissors, Player 1 wins!");
}
else if (user2Input == 1 && user1Input == 3)
{
System.out.println("Player 1 chose Scissors");
System.out.println("Player 2 chose Rock");
System.out.println("Rock Breaks Scissors, Player 2 wins!");
}
//paper covers rock
else if (user1Input == 2 && user2Input == 1)
{
System.out.println("Player 1 chose Paper");
System.out.println("Player 2 chose Rock");
System.out.println("Paper Covers Rock, Player 1 wins!");
}
else if (user2Input == 2 && user1Input == 1)
{
System.out.println("Player 1 chose Rock");
System.out.println("Player 2 chose Paper");
System.out.println("Paper Covers Rock, Player 2 wins!");
}
//scissors cut paper
else if (user1Input == 3 && user2Input == 2)
{
System.out.println("Player 1 chose Scissors");
System.out.println("Player 2 chose Paper");
System.out.println("Scissors cut Paper, Player 1 wins!");
}
else if (user2Input == 3 && user1Input == 2)
{
System.out.println("Player 1 chose Paper");
System.out.println("Player 2 chose Scissors");
System.out.println("Scissors cut Paper, Player 2 wins!");
}
} else {
System.out.println("Error: Invalid input.nPlease make a valid choice.");
} else {
System.out.println("Error: Invalid input.nPlease make a valid choice.");
System.out.println("Would you like to play again ");
System.out.println("Please enter Y/N");
playagain = keyboard.next();
playagain = playagain.toUpperCase();
playAgainOrNot = playagain.charAt(0);
}
//keep the loop running while the user enter Y
while (playAgainOrNot == 'Y');