Reputation: 169
I commented where the code doesn't work. Why doesn't it? I make sure that calculate()
is ==
to computerInput()
, but it prints out "this doesnt work" !! Why is that?
import java.util.Scanner;
class Drank {
private static String[] userOption = new String[] { "Rock", "Paper", "Scissors" };
private static String[] computerOption = new String[] { "Rock", "Paper", "Scissors" };
public static void main(String[] args) {
System.out.println("Enter: Rock, Paper Or Scissors");
System.out.println(calculate());
System.out.println(computerInput());
result();
}
public static int calculate() {
Scanner input = new Scanner(System.in);
String userInput = input.nextLine();
int calculate = 0;
if (userInput.equals(userOption[0])) {
calculate = 0;
} else if (userInput.equals(userOption[1])) {
calculate = 1;
} else if (userInput.equals(userOption[2])) {
calculate = 2;
}
return calculate;
}
public static int computerInput() {
int ai = (int) (Math.random() * 3);
return ai;
}
public static void result() {
if (calculate() == computerInput()) {
System.out.println("Computer's Pick:" + computerOption[computerInput()]);
} else {
// THIS IS WHERE THE PROBLEM
// IS! It always prints out
// the'else' even when both
// are equal?
System.out.println("This doesnt work");
}
}
}
Upvotes: 0
Views: 122
Reputation: 3679
I made minor changes,(not to call methods twice)
main(){
int c = calculate();
int ci=computerInput();
}
public static void result(int c, int ci){
if(c==ci){
System.out.println("Computer's Pick:" + computerOption[computerInput()] );
} else {
System.out.println("This doesnt work"); //THIS IS WHERE THE PROBLEM IS! It always prints out the'else' even when both are equal?
}
Upvotes: 0
Reputation: 68847
You are calling the methods again, instead of using the reusing their return value.
int calc = calculate();
int comp = computerInput();
System.out.println(calc);
System.out.println(comp);
result(calc, comp); // Reuse them here!
Of course, this requires you to add extra arguments to the result
method.
public static void result(int calc, int comp) // Add params here
{
if(calc == comp) // Compare them here!
{
System.out.println("Computer's Pick:" + computerOption[computerInput()] );
} else
{
System.out.println("This doesnt work");
}
}
While you were calling the methods twice, a random number would be generated twice and user input would be asked twice.
Upvotes: 7
Reputation: 1
Probably you should try this:
public static void result(){
int calculate = calculate();
int computerInput = computerInput();
if(calculate == computerInput){
System.out.println("Computer's Pick:" + computerOption[computerInput] );
} else {
System.out.println("This doesnt work");
}
}
Upvotes: 0
Reputation: 13057
Your code uses a random number generator in computerInput()
. This value changes each time you run the method. Therefore you have to be lucky to guess the computer answer. But you obviously don't guess it correctly.
You are calling computerInput()
againt, which means that even if you guess the right value, the displayed result might be different.
Upvotes: 1
Reputation: 41188
I can't see any reason in the code why the comparison wouldn't work.
Note though that each time you call computerInput it will return a different value, so the value it prints will not be the same one used in the comparison.
You need to store the result from computerInput somewhere to show the same number in both places.
Upvotes: 0