S
S
SuperDuperPuper2020-12-09 23:08:36
Java
SuperDuperPuper, 2020-12-09 23:08:36

Why does the instruction in Java not work correctly?

import java.util.Scanner;

public class test {
    static public void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Желаете еще партейку? y/n");
        String answer = scanner.nextLine();
        if (answer == "yes" || answer == "y") {
            System.out.println("ок");
        } else {
            System.out.println(answer);
            System.out.println("Не правильно");

        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Roo, 2020-12-09
@SuperDuperPuper

Strings are compared using the equals method.
For example: answer.equals("yes") , or even better "yes".equals(answer)

J
Jacen11, 2020-12-09
@Jacen11

First, in Java everything works correctly.
Secondly, there are no instructions in Java.
Thirdly, it’s still useful to write not in a notepad, but in an id
. Fourthly, you don’t know what “==” is

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question