samedi 12 novembre 2016

Rock, Paper, Scissors Game - ignored if Statements [Java]

I'm pretty new to programming and I try to program a simple rock, paper, scissors game. The problem is that no matter what the player types it always returns the default message, although the answer should be valid. Could you give me a hint in the right direction? :)

import java.util.Scanner;

public class Game {

//this is the method to evaluate the result

public static void compare (String choice1, String choice2){ 
        if(choice1.toUpperCase() == choice2.toUpperCase()){
            System.out.println("Es ist ein Unentschieden!");
        } 
        else if (choice1.toUpperCase() == "STEIN"){
            if(choice2.toUpperCase() == "Papier"){
                System.out.println("Verloren!");
            } else {
                System.out.println("Gewonnen!");
            }
        } 
        else if (choice1.toUpperCase() == "PAPIER"){
            if(choice2.toUpperCase()=="Stein"){
                System.out.println("Gewonnen!");
            }else{
                System.out.println("Verloren!");
            }
        } 
        else if (choice1.toUpperCase() == "SCHERE"){
            if(choice2.toUpperCase()=="Stein"){
                System.out.println("Verloren!");
            }else{
                System.out.println("Verloren!");
            }
        } 
        else {
            System.out.println("Bitte wiederhole die Eingabe.");
        }
    }

public static void main(String[] args){

    //User Choice
    Scanner userInput = new Scanner(System.in);
    System.out.print("Deine Wahl: ");
    String userChoice = userInput.next(); 
    userInput.close();

    //Computer Choice
    double rand = Math.random(); 
    String computerChoice;
    if (rand < 0.34){
        computerChoice = "Stein"; 
    } else if ( rand < 0.67){
        computerChoice = "Papier";
    } else {
        computerChoice = "Schere";
    } 

    System.out.println("Wahl des Computers: " + computerChoice);

    compare(userChoice, computerChoice);
}

So if I type "Schere", "schere" or "SCHERE" it still returns "Bitte wiederhole die Eingabe". Sorry for all the german in code :) Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire