mercredi 26 septembre 2018

Why is my boolean expression not returning true and printing my statement? [duplicate]

This question already has an answer here:

package rockPaperScissors;

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

public class RockPaperScissors {

public static int userScore = 0;
public static int compScore = 0;

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Welcome to Rock Paper Scissors Let's play best 2 out of 3!");
    System.out.println("Choose your weapon! Rock, Paper, or Scissors");
    String weaponChoice = keyboard.nextLine();

    if(weaponChoice.equals("Rock")) {
        System.out.println("You Chose Rock");
    } else if(weaponChoice.equals("Paper")) {
        System.out.println("You Chose Paper");
    } else if(weaponChoice.equals("Scissors")) {
        System.out.println("You chose Scissors");
    } else {
        System.out.println("That's not a valid answer the computer gets a point!");
        compScore++;
    }

    System.out.println("The computer chose " + getComputerChoice());

if((getComputerChoice() == "Rock") && (weaponChoice == "Rock")) {
    System.out.println("It's a draw!");
}

}
public static String getComputerChoice() {
      String[] choices = {"Rock", "Paper", "Scissors"};
      int randomNumber = (int) Math.floor(Math.random()*3);
      return choices[randomNumber];
    }
}

The user inputs Rock and when the computer randomly generates Rock it should match and be true and then print out It's a draw. It's not printing out so it's somehow returning False. I tried to change the && to || but then it was returning True even when the weaponChoice and getComputerChoice() are different.

I also tried this code

if((getComputerChoice() == weaponChoice)) {
    System.out.println("It's a draw!");
    }

It still won't print anything

I also tried

if(getComputerChoice().equals(weaponChoice)) {
    System.out.println("It's a draw!");
    }

Still nothing

Aucun commentaire:

Enregistrer un commentaire