Results 1 to 8 of 8
Thread: java rock paper scissors code!!!
- 02-12-2012, 11:16 PM #1
Member
- Join Date
- Feb 2012
- Location
- Virginia
- Posts
- 10
- Rep Power
- 0
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?
Java 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(); } }
- 02-13-2012, 01:35 AM #2
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
- 02-13-2012, 01:39 AM #3
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
To correct this change the if statements in the end to char. Also some code is missing in the while loops.
- 02-13-2012, 01:40 AM #4
Re: java rock paper scissors code!!!
Norm beated me to it ... have to write faster =P
- 02-13-2012, 08:25 PM #5
Member
- Join Date
- Feb 2012
- Location
- Virginia
- Posts
- 10
- Rep Power
- 0
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 !
- 02-13-2012, 08:28 PM #6
Re: java rock paper scissors code!!!
You'll have to show us if you want help with it.it only made it worse !
- 02-13-2012, 08:35 PM #7
Member
- Join Date
- Feb 2012
- Location
- Virginia
- Posts
- 10
- Rep Power
- 0
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.
- 02-13-2012, 08:36 PM #8
Similar Threads
-
Rock Paper Scissors Code...
By Shaeman111 in forum New To JavaReplies: 8Last Post: 02-08-2012, 03:20 AM -
Rock paper scissors.
By katie_gsu in forum New To JavaReplies: 7Last Post: 11-29-2011, 02:22 AM -
Help! (Java GUI - Rock, Paper, Scissors)
By Snowball in forum New To JavaReplies: 5Last Post: 06-16-2011, 06:37 AM -
Rock Paper Scissors
By 54byler in forum Advanced JavaReplies: 2Last Post: 04-23-2009, 06:23 AM -
Need Help with Rock paper and Scissors Java Game
By kingsun in forum New To JavaReplies: 3Last Post: 11-17-2008, 03:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks