Java problem

  • From: "Gilbert Neiva" <gneiva@xxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 24 Nov 2010 10:02:05 -0700

I am recreating the game Rock Paper Scissors, but there is a problem. The game 
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 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 


// Computer makes choice 

compChoice = (int)(3.0 * Math.random()) + 1;


// If user chooses R

if (userChoice == "r")

{

userChoice = userChoice.toLowerCase(); // Switch userChoice to lowercase

rockPaperScissors = 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 lowercase 

rockPaperScissors = 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 lowercase 

rockPaperScissors = 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 ties

System.out.println("You have " + wins + " wins and " + ties + " ties.");

}

} // End of class



What am I doing wrong?



Gilbert Neiva


Other related posts: