-1

I am having a problem with some code I wrote for a rock paper scissors game. In my main method I have another method that decides the winner and it isn't performing that method. It's acting as if it isn't even there. Sorry if there is an obvious answer I am just a beginner. For example if it is a tie it won't perform the tie method inside of the decideWinner() method. Thanks for any help

import java.util.Scanner;
import java.util.Random;

public class appels {
public static Scanner Andrew = new Scanner(System.in);
public static Random Hello = new Random();
public static double compAnswer;
public static String compChoice;
public static String answer;
public static void main(String [] args){
getAnswer(answer);
showCompAnswer();
decideWinner();
}
public static void getAnswer(String answer){
System.out.println("Welcome to Rock, Paper, Scissors Vs. the computer.\nWhich do you     choose:");
answer = Andrew.nextLine();
}
public static void showCompAnswer(){
compAnswer = Hello.nextDouble();
//System.out.println(compAnswer);
if(compAnswer > .66){
    compChoice = "rock";
}else if(compAnswer>.33 & compAnswer<.66){
    compChoice = "paper";
}else{
    compChoice = "scissors";
}
System.out.println(compChoice);
}
public static void decideWinner(){
if(compChoice == answer){
    tie();
    System.out.println("Hi");
}
}
public static void tie(){
System.out.println("It's a tie.");
}
public static void compWin(){
System.out.println("The computer wins. :(");
}
public static void userWin(){
System.out.println("You win!");
}
}
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – awksp Jul 02 '14 at 23:47
  • 1
    That's not the only problem, though... Wasn't sure if I should close as dupe or typo, because 1) You never call anything besides `tie()` at the end, and 2) parameter assignment, among other things. `main()` is *definitely* executing exactly what you told it to execute. There are just other mistakes which cause the program's output to not be what you expect. – awksp Jul 02 '14 at 23:47
  • you never call to `compWin` or `userWin`, also you're comparing strings wrong and have 2 `answer` variables - one of them is not used – Iłya Bursov Jul 02 '14 at 23:48
  • 1
    Don't use `&` for and, use `&&`. – BitNinja Jul 02 '14 at 23:49
  • compChoice.equalsIgnoreCase(answer) – Typo Jul 02 '14 at 23:49
  • 1
    Actually, the "and" doesn't need to be there at all. – Dawood ibn Kareem Jul 02 '14 at 23:56

2 Answers2

0

Your final method decideWinner is being called, however the code inside that method is bad and incomplete. First of all when comparing strings you should use if(compChoice.equals(answer)) Secondly that only covers the tie scenario you still need to call the compWin and the userWin methods so you need to add some else ifs.

** Update ** Took the liberty of cleanng up your code a little bit:

import java.util.Scanner;
import java.util.Random;

public class Appels {
    public static Scanner Andrew = new Scanner(System.in);
    public static Random Hello = new Random();
    public static double compAnswer;
    public static String compChoice;
    public static String answer;
    public static void main(String [] args){
        getAnswer();
        showCompAnswer();
        decideWinner();
    }
    public static void getAnswer(){
        System.out.println("Welcome to Rock, Paper, Scissors Vs. the computer.\nWhich do you choose:");
        answer = Andrew.nextLine();
    }
    public static void showCompAnswer() {
        compAnswer = Hello.nextDouble();
        if(compAnswer > .66){
            compChoice = "rock";
        } else if(compAnswer > .33) {
            compChoice = "paper";
        } else{
            compChoice = "scissors";
        }
        System.out.println(compChoice);
    }
    public static void decideWinner(){
        if(compChoice.equals(answer)){
            tie();
        } else if ((compChoice.equalsIgnoreCase("rock") && answer.equals("scissors")) ||
                (compChoice.equalsIgnoreCase("paper") && answer.equals("rock")) ||
                (compChoice.equalsIgnoreCase("scissors") && answer.equals("paper"))) {
            compWin();
        } else {
            userWin();
        }

    }
    public static void tie(){
        System.out.println("It's a tie.");
    }
    public static void compWin(){
        System.out.println("The computer wins. :(");
    }
    public static void userWin(){
        System.out.println("You win!");
    }
}
Uncle Iroh
  • 5,748
  • 6
  • 48
  • 61
0

You can't compare string using == try to use equals(String) method.

Your parameter passing is not correct, you don't need to use a string parameter to getAnswer(String answer) method. Write it without parameters.

change your code as follows:

public static void getAnswer() {
    System.out.println("Welcome to Rock, Paper, Scissors Vs. the computer.\nWhich do you     choose:");
    answer = Andrew.nextLine();
}

public static void decideWinner() {
    if (compChoice.equals(answer)) {
        tie();
        System.out.println("Hi");
    }
}

Read this thread related to string comparing. How do I compare strings in Java?

Community
  • 1
  • 1
user3717646
  • 436
  • 3
  • 10