Hello, I would like to ask a question, I just started programming, trying to do something like a calculator, while stuck on the if statement, I want using input ie. Scanner, enter a certain sign and set it in the variable "znak" to determine what to do, multiply, add

public static void main(String[] args) { Scanner scan = new Scanner(System.in); String znak; int a,b,result; System.out.println("Привет, это калькулятор чисел.\nВведите что вы хотите сделать."); System.out.println("Сложение +"); znak = scan.nextLine(); if (znak == "+") { System.out.println("Вы выбрали сложение\n Введите первое число"); } } 
  • In addition to commas, there is another frequently used punctuation mark - a period. - Igor

2 answers 2

Try this: if(znak.equals("+")) .

    Operator == compares links . If you want to compare two objects, you should use the equals method. Do not forget if you are writing your own class and want to compare objects of this class, you should implement your equals method