Re: Java problem

  • From: "Gilbert Neiva" <gneiva@xxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 24 Nov 2010 13:36:43 -0700

I'm still having problems with my Rock Paper Scissors application. I changed the 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 or userChoice.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 problem


First 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. 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



__________
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

Other related posts: