P
P
PRAIT2019-02-19 01:28:15
Java
PRAIT, 2019-02-19 01:28:15

Why does netBeans require me to convert a chain of if statements into a switch statement?

In this case, the program runs without errors. Here is the code:

import java.util.Scanner;
public class SimpleCalculator {
  public static void main(String[] args) {
    System.out.println("Пожалуйста введите A");
    int a = new Scanner(System.in).nextInt();
    System.out.println("Пожалуйста введите B");
    int b = new Scanner(System.in).nextInt();
    System.out.println("Поажлуйста введите оператор {+,-,*,/}");
    char ch = new Scanner(System.in).nextLine().charAt(0);
    if (ch == '+') {
      System.out.println("a + b = " + (a + b));
    } else if (ch == '-') {
      System.out.println("a - b = " + (a - b));
    } else if (ch == '*'){
      System.out.println("a * b = " + (a * b));
    } else if (ch == '/') {
      System.out.println("a / b = " + (a / b));
    } else {
      System.out.println("Неподдерживаемый оператор: " + ch);
    }
  }
}

5c6b317399c35979853097.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2019-02-19
@PRAIT

The IDE does not require, but only recommends using the switch case.
Since you are setting a rule for a single char variable in this case, you can simplify the readability of your code with a switch case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question