I have updated the source code again. This time, the application works perfectly except for one thing. When I type s for scissors, the application ends. The application is suppose to end only if the user types in any key other the r, p, or s. Here is the application as I ran it.
Enter r for rock, p for paper, or s for scissors or any other key to quit: r You chose rock. I chose paper. Rock, Paper, Scissors Paper covers rock: I win! Enter r for rock, p for paper, or s for scissors or any other key to quit: r We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: r You chose rock. I chose paper. Rock, Paper, Scissors Paper covers rock: I win! Enter r for rock, p for paper, or s for scissors or any other key to quit: p You chose paper. I chose scissors. Rock, Paper, Scissors Scissors cuts paper: I win! Enter r for rock, p for paper, or s for scissors or any other key to quit: p We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: p We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: p You chose paper. I chose scissors. Rock, Paper, Scissors Scissors cuts paper: I win! Enter r for rock, p for paper, or s for scissors or any other key to quit: s You chose scissors. I chose rock. Rock, Paper, Scissors Rock smashes scissors: I win! Thanks for playing. You have 0 wins and 3 ties. Here is the source code. import java.util.Scanner; public class RockPaperScissorsDriver { public static void main(String[] args) { RockPaperScissors userGame; // Holds user's enumeration value RockPaperScissors compGame; // Holds computer's enumeration value final String TITLE = "Rock, Paper, Scissors"; // Game chant final String PAPERCOVERS = "Paper covers rock:"; final String SCISSORSCUTS = "Scissors cuts paper:"; final String ROCKSMASH = "Rock smashes scissors:"; final String IWIN = "I win!"; final String YOUWIN = "You win!"; String userChoice; // Holds user's choice int userNumberChoice; int compChoice; // Holds computer's choice int wins = 0; // Holds user's number of wins int ties = 0; // Holds number of ties between user and computer Scanner inData = new Scanner(System.in); // Scanner object do { // Print statement asks user to enter choice System.out.println("Enter r for rock, p for paper, or s for scissors " + "or any other key to quit:"); userChoice = inData.nextLine(); // Get userChoice userChoice = userChoice.toLowerCase(); // Switch userChoice to lowercase // Computer makes choice compChoice = (int)(3.0 * Math.random()) + 1; // If user chooses r and computer chooses 2 if ((userChoice.equals("r")) && (compChoice == 2)) { userNumberChoice = 1; // Set to 1 compChoice = 2; userGame = RockPaperScissors.rock; // Change value of userGame compGame = RockPaperScissors.paper; // Change value of compGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(PAPERCOVERS); // Print Paper covers rock // Print statement tells user computer wins System.out.println(IWIN); } // If user chooses p and computer chooses 3 else if ((userChoice.equals("p")) && (compChoice == 3)) { userNumberChoice = 2; // Set to 2 compChoice = 3; userGame = RockPaperScissors.paper; // Change value of userGame compGame = RockPaperScissors.scissors; // Change value of compGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(SCISSORSCUTS); // Print scissors cuts paper // Print statement tells user computer wins System.out.println(IWIN); } // If user chooses s and computer chooses 1 else if ((userChoice.equals("s")) && (compChoice == 1)) { userNumberChoice = 3; // Set to 3 compChoice = 1; userGame = RockPaperScissors.scissors; // Change value of userGame compGame = RockPaperScissors.rock; // Change value of compGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(ROCKSMASH); // Print rock smashes scissors // Print statement tells user computer wins System.out.println(IWIN); } // If computer chooses 1 and user chooses p else if ((compChoice == 1) && (userChoice.equals("p"))) { compChoice = 1; userNumberChoice = 2; // Set to 2 compGame = RockPaperScissors.rock; // Change value of compGame userGame = RockPaperScissors.paper; // Change value of userGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(PAPERCOVERS); // Print paper covers rock // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 } // If computer chooses 2 and user chooses s else if ((compChoice == 2) && (userChoice.equals("s"))) { compChoice = 2; userNumberChoice = 3; // Set to 3 compGame = RockPaperScissors.paper; // Change value of compGame userGame = RockPaperScissors.scissors; // Change value of userGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(SCISSORSCUTS); // Print scissors cuts paper // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 } // Ifcomputer chooses 3 and user chooses r else if ((compChoice == 3) && (userChoice.equals("r"))) { compChoice = 3; userNumberChoice = 1; // Set to 1 userGame = RockPaperScissors.rock; // Change value of userGame compGame = RockPaperScissors.scissors; // Change value of compGame //Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); //Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(ROCKSMASH); // Print rock smashes scissors // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 }else if ((userChoice.equals("r") && compChoice == 1) || (userChoice.equals("p") && compChoice == 2) ||
(userChoice.equals("s") && compChoice == 3)) { // Print statement tells user is tied with computer System.out.println("We are tied!"); ties++; // Increment ties by 1 } } // Until user chooses r, p, or swhile ((userChoice.compareTo("r") < 0) || (userChoice.compareTo("p") < 0) || (userChoice.compareTo("s") < 0));
// Print statement ending game System.out.println("Thanks for playing."); // Print statement tells user number of wins and number of ties System.out.println("You have " + wins + " wins and " + ties + " ties."); } } // End of classThe compareTo() method still confuses me. I don't know whether to put compareTo(argument) > 0 or compareTo(argument) < 0. I think I've gotten the hang of the .equals() method. So why does the application end when I type s.
Gilbert Neiva----- Original Message ----- From: "Dave" <davidct1209@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx> Sent: Wednesday, November 24, 2010 2:42 PM Subject: Re: Java problem
It's exactly what the error says: else if (userChoice.compareTo("r") > 0 or userChoice.compareTo("p") > 0What's wrong with this expression? Read up on boolean expressions as a hint.It might also help to think about another way to represent the game states:- why do you have a string representation for one and a numbered representation for another? - can you think of a way to save the outcome of the game given the two game states using a two dimensional array? On 11/24/10, Gilbert Neiva <gneiva@xxxxxxx> wrote:I'm still having problems with my Rock Paper Scissors application. I changedthe program code in the driver class as follows. import java.util.Scanner; public class RockPaperScissorsDriver { public static void main(String[] args) { RockPaperScissors userGame; // Holds user's enumeration value RockPaperScissors compGame; // Holds computer's enumeration value final String TITLE = "Rock, Paper, Scissors"; // Game chant final String PAPERCOVERS = "Paper covers rock:"; final String SCISSORSCUTS = "Scissors cuts paper:"; final String ROCKSMASH = "Rock smashes scissors:"; final String IWIN = "I win!"; final String YOUWIN = "You win!"; String userChoice; // Holds user's choice int compChoice; // Holds computer's choice int wins = 0; // Holds user's number of wins int ties = 0; // Holds number of ties between user and computer Scanner inData = new Scanner(System.in); // Scanner object do { // Print statement asks user to enter choice System.out.println("Enter r for rock, p for paper, or s for scissors " + "or any other key to quit:"); userChoice = inData.nextLine(); // Get userChoice userChoice = userChoice.toLowerCase(); // Switch userChoice to lowercase // Computer makes choice compChoice = (int)(3.0 * Math.random()) + 1; // If user chooses r and computer chooses 2 if ((userChoice.equals("r")) && (compChoice == 2)) { userGame = RockPaperScissors.rock; // Change value of userGame compGame = RockPaperScissors.paper; // Change value of compGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(PAPERCOVERS); // Print Paper covers rock // Print statement tells user computer wins System.out.println(IWIN); } // If user chooses p and computer chooses 3 else if ((userChoice.equals("p")) && (compChoice == 3)) { userGame = RockPaperScissors.paper; // Change value of userGame compGame = RockPaperScissors.scissors; // Change value of compGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(SCISSORSCUTS); // Print scissors cuts paper // Print statement tells user computer wins System.out.println(IWIN); } // If user chooses s and computer chooses 1 else if ((userChoice.equals("s")) && (compChoice == 1)) { userGame = RockPaperScissors.scissors; // Change value of userGame compGame = RockPaperScissors.rock; // Change value of compGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(ROCKSMASH); // Print rock smashes scissors // Print statement tells user computer wins System.out.println(IWIN); } // If computer chooses 1 and user chooses p else if ((compChoice == 1) && (userChoice.equals("p"))) { compGame = RockPaperScissors.rock; // Change value of compGame userGame = RockPaperScissors.paper; // Change value of userGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(PAPERCOVERS); // Print paper covers rock // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 } // If computer chooses 2 and user chooses s else if ((compChoice == 2) && (userChoice.equals("s"))) { compGame = RockPaperScissors.paper; // Change value of compGame userGame = RockPaperScissors.scissors; // Change value of userGame // Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); // Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(SCISSORSCUTS); // Print scissors cuts paper // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 } // Ifcomputer chooses 3 and user chooses r else if ((compChoice == 3) && (userChoice.equals("r"))) { userGame = RockPaperScissors.rock; // Change value of userGame compGame = RockPaperScissors.scissors; // Change value of compGame //Print statement tells user his or her choice System.out.println("You chose " + userGame + "."); //Print statement tells user computer's choice System.out.println("I chose " + compGame + "."); System.out.println(TITLE); // Print game chant System.out.println(ROCKSMASH); // Print rock smashes scissors // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 }else if (userChoice.compareTo("r") > 0 or userChoice.compareTo("p") > 0 oruserChoice.compareTo("s") > 0) { //Print statement ending game System.out.println("Thanks for playing."); } else // Otherwise { // Print statement tells user is tied with computer System.out.println("We are tied!"); ties++; // Increment ties by 1 } } // Until user chooses r, p, or s while (userChoice.compareTo("r") > 0 or userChoice.compareTo("p") > 0 or userChoice.compareTo("s") > 0); // Print statement tells user number of wins and number of ties System.out.println("You have " + wins + " wins and " + ties + " ties."); } } // End of class The enum class is still the same. // Enumeration RockPaperScissors contains fields for the game rock, // paper, scissors. public enum RockPaperScissors { rock, paper, scissors } When I try to compile the application, I get the following error. C:\Users\user\Desktop\COMP268\Practice\RockPaperScissors\src>javac *.java RockPaperScissorsDriver.java:143: ')' expected else if (userChoice.compareTo("r") > 0 or userChoice.compareTo("p") > 0 or userChoice.compareTo("s") > 0) ^ RockPaperScissorsDriver.java:154: illegal start of expression } ^ RockPaperScissorsDriver.java:157: ')' expected while (userChoice.compareTo("r") > 0 or userChoice.compareTo("p") > 0 or userCho ice.compareTo("s") > 0); ^ 3 errors C:\Users\user\Desktop\COMP268\Practice\RockPaperScissors\src> graphic 827 I am totally stumped on what to do. Can someone help me out with this problem? Thanks. Gilbert Neiva ----- Original Message ----- From: "Dave" <davidct1209@xxxxxxxxx> To: <programmingblind@xxxxxxxxxxxxx> Sent: Wednesday, November 24, 2010 10:42 AM Subject: Re: Java problemFirst issue is check the way you're comparing string; are you comparing string object references or performing a case-sensitive character match/case-insensitive match? Read up on it if that didn't make sense (either below or in any intro java book under any discussion of strings). http://leepoint.net/notes-java/data/strings/12stringcomparison.html On 11/24/10, Gilbert Neiva <gneiva@xxxxxxx> wrote:I am recreating the game Rock Paper Scissors, but there is a problem. Thegame doesn't run properly. I get an infinate loop, and the game doesn't tell me my or it's choise. Here is what happens when I run it. Enter r for rock, p for paper, or s for scissors or any other key to quit: r Thanks for playing. We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: s Thanks for playing. We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: p Thanks for playing. We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: P Thanks for playing. We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: S Thanks for playing. We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: q Thanks for playing. We are tied! Enter r for rock, p for paper, or s for scissors or any other key to quit: I have two class files, one with an enum class and the other is the driver class. Here is the code for the enum class. // Enumeration RockPaperScissors contains fields for the game rock, // paper, scissors. public enum RockPaperScissors { rock, paper, scissors } Here is the source code for the driver class. import java.util.Scanner; public class RockPaperScissorsDriver { public static void main(String[] args) { RockPaperScissors rockPaperScissors; // Holds enumeration value final String TITLE = "Rock, Paper, Scissors"; // Game chant final String PAPERCOVERS = "Paper covers rock:"; final String SCISSORSCUTS = "Scissors cuts paper:"; final String ROCKSMASH = "Rock smashes scissors:"; final String IWIN = "I win!"; final String YOUWIN = "You win!"; String userChoice; // Holds user's choice int compChoice; // Holds computer' int wins = 0; // Holds user's number of wins int ties = 0; // Holds number of ties between user and computer Scanner inData = new Scanner(System.in); // Scanner object do { // Print statement asks user to enter choiceSystem.out.println("Enter r for rock, p for paper, or s for scissors " +"or any other key to quit:"); userChoice = inData.nextLine(); // Get userChoice // Computer makes choice compChoice = (int)(3.0 * Math.random()) + 1; // If user chooses R if (userChoice == "r") {userChoice = userChoice.toLowerCase(); // Switch userChoice to lowercaserockPaperScissors = RockPaperScissors.rock; // Change value of rockPaperScissors //Print statement tells user his or her choice System.out.println("You chose " + rockPaperScissors + "."); } // If user chooses p else if (userChoice == "p") {userChoice = userChoice.toLowerCase(); // Switch userChoice to lowercaserockPaperScissors = RockPaperScissors.paper; // Change value of rockPaperScissors // Print statement tells user his or her choice System.out.println("You chose " + rockPaperScissors + "."); } // If user chooses s else if (userChoice == "s") {userChoice = userChoice.toLowerCase(); // Switch userChoice to lowercaserockPaperScissors = RockPaperScissors.scissors; // Change value of rockPaperScissors // Print statement tells user his or her choice System.out.println("You chose " + rockPaperScissors + "."); } else // Otherwise { // Print statement ending game System.out.println("Thanks for playing."); compChoice = 0; // Reset compChoice to 0 } // If computer chooses 1 if (compChoice == 1) { rockPaperScissors = RockPaperScissors.rock; // Change value of rockPaperScissors // Print statement tells user computer's choice System.out.println("I chose " + rockPaperScissors + "."); } // If computer chooses 2 else if (compChoice == 2) { rockPaperScissors = RockPaperScissors.paper; // Change value of rockPaperScissors // Print statement tells user computer's choice System.out.println("I chose " + rockPaperScissors + "."); } // If computer chooses 3 else if (compChoice == 3) { rockPaperScissors = RockPaperScissors.scissors; // Change value of rockPaperScissors // Print statement tells user computer's choice System.out.println("I chose " + rockPaperScissors + "."); } // If user chooses r and computer chooses 2 if ((userChoice == "r") && (compChoice == 2)) { System.out.println(TITLE); // Print game chant System.out.println(PAPERCOVERS); // Print Paper covers rock // Print statement tells user computer wins System.out.println(IWIN); } // If user chooses p and computer chooses 3 else if ((userChoice == "p") && (compChoice == 3)) { System.out.println(TITLE); // Print game chant System.out.println(SCISSORSCUTS); // Print scissors cuts paper // Print statement tells user computer wins System.out.println(IWIN); } // If user chooses s and computer chooses 1 else if ((userChoice == "s") && (compChoice == 1)) { System.out.println(TITLE); // Print game chant System.out.println(ROCKSMASH); // Print rock smashes scissors // Print statement tells user computer wins System.out.println(IWIN); } // If computer chooses 1 and user chooses p else if ((compChoice == 1) && (userChoice == "p")) { System.out.println(TITLE); // Print game chant System.out.println(PAPERCOVERS); // Print paper covers rock // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 } // Ifcomputer chooses 2 and user chooses s else if ((compChoice == 2) && (userChoice == "s")) { System.out.println(TITLE); // Print game chant System.out.println(SCISSORSCUTS); // Print scissors cuts paper // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 } // If computer chooses 3 and user chooses r else if ((compChoice == 3) && (userChoice == "r")) { System.out.println(TITLE); // Print game chant System.out.println(ROCKSMASH); // Print rock smashes scissors // Print statement tells user he or she wins System.out.println(YOUWIN); wins++; // Increment wins by 1 } else // Otherwise { // Print statement tells user is tied with computer System.out.println("We are tied!"); ties++; // Increment ties by 1 } } // Until user chooses r, p, or s while ((userChoice != "r") || (userChoice != "p") || (userChoice != "s")); // Print statement tells user number of wins and number of tiesSystem.out.println("You have " + wins + " wins and " + ties + " ties.");} } // End of class What am I doing wrong? Gilbert Neiva__________ View the list's information and change your settings at //www.freelists.org/list/programmingblind__________ View the list's information and change your settings at //www.freelists.org/list/programmingblind__________ View the list's information and change your settings at //www.freelists.org/list/programmingblind
__________View the list's information and change your settings at //www.freelists.org/list/programmingblind