-1
package org.example.ChallengeGame;

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

public class Main {
    public static void main(String[] args) {

        //TODO Opening message for user
        System.out.println("Welcome to the guessing game!\n" +
                "We will provide a random number and you will guess what the number is.\n" +
                "Ready to begin? Press \"Yes\" if not type \"No\" \n");


        // TODO Initialize random integer
        Random random = new Random();
        int rand_int = random.nextInt(10) + 1;

        // TODO Get user input
        Scanner scanner = new Scanner(System.in);
        String user_ans = scanner.next();

        System.out.println("User answer is: " + user_ans);
        
        // This part is me checking if I type in "Yes" in my program will it work but it returns as false still
        if(user_ans == "Yes") {
            System.out.println("true");
        }else{
            System.out.println("false");
        }

        // This is my end goal so I can continue finishing my exercise
        /*if(user_ans == "Yes") {
            do {
                System.out.println("Start Game");
            } while (user_ans == "Yes");
        }else{
            System.out.println("Exit program");
        }*/
    }
}
Marvin
  • 12,215
  • 3
  • 50
  • 50
  • You want to use `userAnswer.equalsIgnoreCase("yes")` for example, to ignore the casing of the user input. And: learn about java naming conventions. You do NOT use the _ in names (unless SOME_CONSTANT). Use camelCase instead. – GhostCat Apr 12 '22 at 14:25

1 Answers1

-1

So first off, I would recommend you use scanner.nextLine() instead, which will everything you type until you hit enter. Additionally, you are trying to compare Strings using the == operator, which you shouldn't do - use user_ans.Equals("Yes") instead. In Java, Strings are stored as objects, so using == compares the reference of the object to the String ("Yes" in this case). Using .equals("Yes"), you make sure the actual content of the String variable is compared instead.

David Hutter
  • 21
  • 1
  • 4
  • 1
    I would recommend to not answer questions that have been asked a million times before, and that are closed as duplicate within minutes. – GhostCat Apr 12 '22 at 14:25