-2
import java.util.Scanner;
public class guessGame {
    public static void main(String[] args){
        String ansOne;
        String ansTwo;
        String result="";

        Scanner sc = new Scanner(System.in);
        System.out.println("TWO QUESTIONS!");
        System.out.println("Think of an object and I'll try to guess it");
        System.out.println("Qusetion 1) Is it a animal,vegetable or mineral?  ");
        ansOne = sc.next();
        System.out.println("Qusetion 2) Is it bigger than a bread box");
        ansTwo = sc.next();

        if(ansOne == "animal" && ansTwo == "yes"){
            result = "Squirrel";
        }
        else if( ansOne == "animal" && ansTwo == "no"){
            result = "moose";
        }
            else if( ansOne == "Vegetable" && ansTwo == "Yes"){
            result = "carrot";
        }
            else if( ansOne == "Vegetable" && ansTwo == "no"){
            result = "Watermelon";
        }
            else if( ansOne == "mineral" && ansTwo == "Yes"){
            result = "Paper clip";
        }
            else if( ansOne == "mineral" && ansTwo == "no"){
            result = "Camero";
        }
        System.out.println("My guess is that You are thinking of a " +result);
        System.out.println("I would ask you if I'm right,but I don't actually care...");  
    }  
}

In the above code,I want to display the result. But its not been displayed, I am getting the following output:

TWO QUESTIONS! Think of an object and I'll try to guess it Qusetion 1) Is it a animal,vegetable or mineral?
vegetable Qusetion 2) Is it bigger than a bread box yes My guess is that You are thinking of a
I would ask you if I'm right,but I don't actually care...

Pieter De Bie
  • 1,074
  • 13
  • 30
kiruthika
  • 1
  • 2

2 Answers2

1

i think you should use sc.nextLine() rather than sc.next(). Additionally use equals() rather than '=='

achin
  • 169
  • 8
0

You have to compare your strings with equals ()

E.g. String1.equals("Lalala");

You wil get unexpected results if you use == for comperision.

Marcinek
  • 2,144
  • 1
  • 19
  • 25