0

I am new to Java and am trying to make an interactive program which asks you three riddles (the actual program is inspired by The Batman (movie)'s official website www.rataalada.com.

I have initialised the strings, which are meant to be the answers for the riddles. And I have also tried obtaining the input of the user (their answer to the riddle). However, the program does not work and I don't know where I went wrong. Below is my program:

import java.util.concurrent.TimeUnit;

public class rataAlada{

public static void main(String[] args){

    Scanner in = new Scanner(System.in);

    String answer1 = "He lies still";
    String answer2 = "Justice";
    String answer3 = "A friend";
    String guess1;
    String guess2;
    String guess3;

    //initial talk
    System.out.println("Hello there.....");
    TimeUnit.SECONDS.sleep(2);
    System.out.println("I've been waiting for your arrival.....");
    TimeUnit.SECONDS.sleep(2);
    System.out.println("You might think you are smart, finding this. But if you really are smart, you would be able to solve these riddles.");
    TimeUnit.SECONDS.sleep(4);

    //riddle number one
    System.out.println("Number ONE.");
    System.out.println("What does a liar do when he's dead?");
    guess1 = in.nextLine();
    if (guess1 == answer1){
        System.out.println("Correct! But wait, there are still two more.....");
    }
    else{
        System.out.println("You really shouldn't be here. But I pity you. TRY AGAIN.");
        guess1 = in.nextLine();
    }

    //riddle number two
    System.out.println("Number TWO.");
    System.out.println("It can be cruel, poetic, or blind. But when it is denied, its violence you may find.");
    guess2 = in.nextLine();
    if (guess2 == answer2){
        System.out.println("Good, good, but are you good enough?");
    }
    else{
        System.out.println("We don't have all day. Make another guess.");
        guess2 = in.nextLine();
    }

    //riddle number three
    System.out.println("Number THREE.");
    System.out.println("The less of me you have, the more I am worth. What am I?");
    guess3 = in.nextLine();
    if (guess3 == answer3){
        System.out.println("You are actually smart. But this isn't the end....");
        System.out.println("Visit www.rataalada.com. Your next challenge awaits......");
    }
    else{
        System.out.println("I would kick you off this program but I'm feeling nice. TRY AGAIN.");
        guess3 = in.nextLine();
    }
}

}

Cashyup
  • 1
  • 2
  • Hello ! Could you explain what happen when you launch your program ? – Elley Apr 06 '22 at 11:40
  • 1
    @Elley no doubt the OP is getting wrong results, because this: if (guess1 == answer1) doesn't do what he thinks it does. – Stultuske Apr 06 '22 at 11:42
  • @Elley When I try to launch the program, it says "Scanner cannot be resolved to a type". – Cashyup Apr 06 '22 at 11:47
  • @Stultuske I am assuming that if the user's input matches the string I initialised. If that's not the case, what does it mean? Should it have one equal sign as it is strings and not numbers? – Cashyup Apr 06 '22 at 11:49
  • 1
    @Cashyup your error means you didn't add a correct import statement for the Scanner class. No. In your comparison, you are comparing the references of the Strings, not their values. You would need to use the equals, or equalsIgnoreCase methods to compare their values – Stultuske Apr 06 '22 at 11:53
  • ok thanks. wait, do i create a for loop first and then an if-else statement? – Cashyup Apr 06 '22 at 13:09

0 Answers0