Ok so I'm making a rock paper scissors program in java and I have all the code and it should give me the output I want but every time i run it it only prints out the input statement and nothing else. I made it print a line after the do loop that the input statement is located and it prints that but it doesn't print anything that's in the if statements and I don't quite understand why. This is what I have. Thank you for any help provided.
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random rand = new Random();
String replay = ("");
String user = ("");
int ai = 0;
String aiPlay = ("");
do
{
ai = rand.nextInt(3) + 1;
switch (ai) //was if statements but java suggested to change to switch statements
{
case 1:
aiPlay = ("Rock");
break;
case 2:
aiPlay = ("Paper");
break;
default:
aiPlay = ("Scissors");
break;
}
do //make sure input is correct
{
System.out.print("rock, paper, or scissors: ");
user = input.next();
}while(!(user.equals("rock") || user.equals("paper") || user.equals("scissors")));
if(user.equals(ai))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nIt was a tie!");
System.out.print("Continue? (y or n): ");
replay = input.next();
}
switch (user) { //was if statements java suggested it change to switch statement
case "rock":
if (aiPlay.equals("scissors"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
System.out.print("Continue? (y or n): ");
replay = input.next();
}
else if (aiPlay.equals("paper"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
System.out.print("Continue? (y or n): ");
replay = input.next();
} break;
case "paper":
if (aiPlay.equals("scissors"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
System.out.print("Continue? (y or n): ");
replay = input.next();
}
else if (aiPlay.equals("rock"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
System.out.print("Continue? (y or n): ");
replay = input.next();
} break;
case "scissors":
if (aiPlay.equals("paper"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
System.out.print("Continue? (y or n): ");
replay = input.next();
}
else if (aiPlay.equals("rock"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
System.out.print("Continue? (y or n): ");
replay = input.next();
} break;
default:
break;
}
}while(replay.equals("y"));
}
switch (ai) //was if statements but java suggested to change to switch
statements
{
case 1:
aiPlay = ("rock"); /* All should be lowercase*/
break;
case 2:
aiPlay = ("paper"); /* All should be lowercase*/
break;
default:
aiPlay = ("scissors"); /* All should be lowercase*/
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.