java rock paper scissors code!!!
So I managed to get some code going, but I can't seem to figure out how to get the if comparisons to work. It stores their inputs, and if it's a tie, it works. But for anything different it doesn't seem to work. How can I compare their inputs?
Code:
import java.util.Scanner;
public class RockPaperScissors4 {
public static void main (String [] args){
Scanner cool = new Scanner(System.in);
String player1;
char player1choice;
String player2;
char player2choice;
System.out.println("Hello Player 1, what is your name?");
player1 = cool.next();
System.out.println(player1 + ", please enter your choice (R for rock, P for paper or S for scissors): ");
player1choice = cool.next().charAt(0);
while(player1choice !='R' && player1choice !='P' && player1choice !='S' && player1choice !='r' && player1choice !='p' && player1choice !='s')
{
System.out.println("That is an incorrect input. Please enter a legitimate response");
player1choice = cool.next().charAt(0);
}
System.out.println("Hello Player 2, what is your name?");
player2 = cool.next();
System.out.println(player2 + ", please enter your choice (R for rock, P for paper or S for scissors): ");
player2choice = cool.next().charAt(0);
while(player2choice !='R' && player2choice !='P' && player2choice !='S' && player2choice !='r' && player2choice !='p' && player2choice !='s')
{
System.out.println("That is an incorrect input. Please enter a legitimate response");
player2choice = cool.next().charAt(0);
}
char R = 0;
char S = 1;
char P = 2;
if (player1choice == player2choice)
System.out.println("Tie!");
else if (player1choice == 0 && player2choice == 1)
System.out.println("Rock beats scissors! You win!");
else if (player1choice == 0 && player2choice == 2)
System.out.println("Paper covers rock... You lose.");
else if (player1choice == 1 && player2choice == 0)
System.out.println("Rock beats scissors... you lose.");
else if (player1choice == 1 && player2choice == 2)
System.out.println("Scissors cuts paper! You win!");
else if (player1choice == 2 && player2choice == 0)
System.out.println("Paper covers rock! You win!");
else if (player1choice == 2 && player2choice == 1)
System.out.println("Scissors cuts paper... You lose.");
else
System.out.println();
}
}
Re: java rock paper scissors code!!!
It looks like you are comparing a char value: player1choice against an int value: 1.
The value of '1' is not equal to 1. Change your compares to be against char values, not int values
Re: java rock paper scissors code!!!
I do not understand your logic at all ... playerchoises will be Char and not an integer between 0 and 2 ... and the char R = 0; things that you are doing has no purpose , numbers can't be used as references in they way that you plan :P:
To correct this change the if statements in the end to char. Also some code is missing in the while loops.
Re: java rock paper scissors code!!!
Norm beated me to it ... have to write faster =P
Re: java rock paper scissors code!!!
Okay... I really really need a visual of how i should do this, i tried to fix it and it only made it worse !
Re: java rock paper scissors code!!!
Quote:
it only made it worse !
You'll have to show us if you want help with it.
Re: java rock paper scissors code!!!
Oh, i tried a lot of things last night but i didn't save them because they didn't help. I guess i'll keep trying.
Re: java rock paper scissors code!!!
If you have questions or problems, post them.