Question:
Trying to make Rock, Paper, Scissors in Java and I keep getting this problem?
therabidliger
2009-09-16 18:15:05 UTC
I'm supposed to get results that look like this:
Rock Paper Scissors
************************************************************
Player name: Ashley
Player hand: rock
Computer generated value: 0.4408697471080901
Computer hand: paper
The computer wins!
************************************************************

However. I can't get the part that says "The computer wins!/Tie!/etc..." to appear (the last line before ****). And in the line above that no matter what I do the computer always has scissors. It's only supposed to have scissors if the value is greater than or equal to 2/3

Here's my code (I know it kind of sucks this is my first real java project):
public class Project1
{
public static void main(String[] args)
{
String name = (args[0]); //name
String hand = (args[1]); //hand
String chand = " ";
String outcome = " ";

double computer = Math.random(); //random number between 0 and 1

//determines computer hand
if (computer < 1/3) {
chand = "rock";
}
if (computer >= 1/3 && computer < 2/3) {
chand = "paper";
}
if (computer >=2/3) {
chand = "scissors";
}

//determines the winner
if (hand == "rock" && chand == "rock") {
outcome = "Tie!";
}
else if (hand == "rock" && chand == "paper") {
outcome = "The computer wins!";
}
else if (hand == "rock" && chand == "scissors") {
outcome = name + " wins!";
}
else if (hand == "paper" && chand == "rock") {
outcome = name + " wins!";
}
else if (hand == "paper" && chand == "paper") {
outcome = "Tie!";
}
else if (hand == "paper" && chand == "scissors") {
outcome = "The computer wins!";
}
else if (hand == "scissors" && chand == "rock") {
outcome = "The computer wins!";
}
else if (hand == "scissors" && chand == "paper") {
outcome = name + " wins!";
}
else if (hand == "scissors" && chand == "scissors") {
outcome = "Tie!";
}

//output
System.out.println("Rock Paper Scissors");
System.out.println("************************************************************");
System.out.println("Player name: " + name);
System.out.println("Player hand: " + hand);
System.out.println("Computer generated value: " + computer);
System.out.println("Computer hand: " + chand);
System.out.println(outcome);
System.out.println("************************************************************");
}
}
Three answers:
Badajoz
2009-09-16 18:48:26 UTC
1. Use .equals() method for comparing Strings:

Wrong: hand == "scissors"

Right: hand.equals("scissors")



Do that for the rest of your conditions that involves String comparison.



2. You can also generate a number larger than 1 if you multiply the random() method:



//Outputs random 0,1 and 2

int computer = (int) (Math.random() * 3);



//Assign

switch(computer){

case 0:

chand = "rock";

break;

case 1:

chand = "paper";

break;

case 2:

chand = "scissors";

break;

}



Hope it helps.
Joe L
2009-09-16 18:45:52 UTC
The first problem that jumps out is that the fractions you are comparing against are using integers. In java, 1/3 = 0, because 1 and 3 are integers and you are getting an integer result. So, the "If computer > 2/3" is always true since 2/3 = 0. That is why you always get scissors.



if (Computer < 0.333)



Your next problem is the way you are comparing the Strings. When you say if (hand == "scissors", you are saying: If the object hand is the same object as the constant String "scissors". Usually, you want to know if objects have the same contents. To do that, use the equals method:



if ("scissors".equals(hand) && "scissors".equals(chand).



Next, I'll give you some suggestions to simplify your logic.
Mark aka jack573
2009-09-20 17:00:28 UTC
As the others have said, you need to change a couple of things in your program.

1.

chand == "rock", etc

should be

chand.equals("rock"), etc



2.

The way you are checking for the random number against 1/3, 2/3.





I have changed your program to have the equals() instead of ==.



I put a new variable named value that holds an int value 0, or 1, or 2.

If the random number is below 1/3, value will be 0.

If the random number is between 1/3 and 2/3, value will be 1.

If the random number is greater than or equal to 2/3,. value will be 2.



I have kept your variable computer so you can output it at the end as you need to.



Here is the changed program, but it does NOT have all of the **** as you probably need, so you will need to change that yourself.



public class Project1

{

public static void main(String[] args)

{

String name = (args[0]); //name

String hand = (args[1]); //hand

String chand = " ";

String outcome = " ";



double computer = Math.random(); //random number between 0 and 1

int value = (int) (computer * 3); // This will give you either 0, 1, or 2.



//determines computer hand

if (value == 0) {

chand = "rock";

}

if (value == 1) {

chand = "paper";

}

if (value == 2) {

chand = "scissors";

}



//determines the winner

if (hand.equals("rock") && chand.equals("rock")) {

outcome = "Tie!";

}

else if (hand.equals("rock") && chand.equals("paper")) {

outcome = "The computer wins!";

}

else if (hand.equals("rock") && chand.equals("scissors")) {

outcome = name + " wins!";

}

else if (hand.equals("paper") && chand.equals("rock")) {

outcome = name + " wins!";

}

else if (hand.equals("paper") && chand.equals("paper")) {

outcome = "Tie!";

}

else if (hand.equals("paper") && chand.equals("scissors")) {

outcome = "The computer wins!";

}

else if (hand.equals("scissors") && chand.equals("rock")) {

outcome = "The computer wins!";

}

else if (hand.equals("scissors") && chand.equals("paper")) {

outcome = name + " wins!";

}

else if (hand.equals("scissors") && chand.equals("scissors")) {

outcome = "Tie!";

}



//output

System.out.println("Rock Paper Scissors");

System.out.println("********************");

System.out.println("Player name: " + name);

System.out.println("Player hand: " + hand);

System.out.println("Computer generated value: " + computer);

System.out.println("Computer hand: " + chand);

System.out.println(outcome);

System.out.println("********************");

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...