-3

I need to solve this exercise using java code:

We have a monkey and a boolean parameter aSmile, we need to know if the monkey is smiling and if it is "We are in trobule" if it is not "We are good".

import java.util.Scanner;


public class taxi {

public static void main(String[] args) {

    boolean aSmile;


    Scanner scan = new Scanner(System.in);
    System.out.println("Is monkey A smiling?");
    String answer = scan.nextLine();
    if (answer.equalsIgnoreCase("yes")){
         aSmile = true; 
    }
    else if (answer.equalsIgnoreCase("no")){
        aSmile = false;
    }
    else {
        System.out.println("Sorry, write a correct answer");
    }


    if(aSmile = true){
        System.out.println("We are in trouble");
    }

    else if (aSmile = false){
        System.out.println("We are good!");
    }
   }
}

The problem is that I always get "We are in trobule", no matter what I write in the console.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
wapetine
  • 1
  • 1
  • 1

3 Answers3

1
if(aSmile = true){ //Look here.
    System.out.println("We are in trouble");
}

You are assigning the value trueto the variable aSmile.

Replace it with :

if(aSmile) { //Or if(aSmile == true) {
  ...
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

You are using an assignment operator and then check for the boolean value.

if(aSmile = true){
        System.out.println("We are in trouble");
    }

aSmile will always be true, because you assign it true and then check if it's true. You have to use the == operator or in this case simply

if(aSmile){ // means if aSmile is true
}
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

You have incorrectly used conditional operator. It should be like

    if(aSmile){

     }
Supun Dharmarathne
  • 1,138
  • 1
  • 10
  • 19