W
W
wolverine7772021-12-08 01:00:32
Java
wolverine777, 2021-12-08 01:00:32

Why can't exit the loop?

Hello, why can't you exit the loop when you enter, stringand if you replace it with int- does everything work?

DOES NOT WORK:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean isNotEnough = true;

        do {
            System.out.println("Please enter the question:");
            String question = sc.nextLine();
            if(question=="out"){
                isNotEnough=false;
            }
        }
        while(isNotEnough);
        System.out.println("Thanks");
    }


WORKS:

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        boolean isNotEnough = true;

        do {
            System.out.println("Please enter the question:");
            int question = sc.nextInt();
            if (question==1){
                isNotEnough=false;
            }
        }
        while(isNotEnough);

        System.out.println("Thanks");

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-12-08
@wolverine777

Because someone didn't read about string comparison in Java .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question