저는 자바 프로그래밍에 익숙하지 않고 방금 계산기 프로그램을 만들었지 만 다른 프로그래머는 계산기 프로그램에서 "많은 구문 분석"을 사용하는 것으로 보입니다. 내가 잘못된 접근법을 취하고 있는지, 그리고 이런 종류의 논리를 사용하여 미래에 문제가 발생할지를 묻고 싶었습니다. 감사합니다.코드 자바 효율성에 대한 조언이 필요합니다.
class Refresh {
private final String a;
private final String b;
private final String c;
private final String d;
private double x, y;
public Refresh() {
a = "Enter the first number: ";
b = "Enter the function; ";
c = "Enter the second number: ";
d = "The result is: ";
}
public void types() {
Scanner typ = new Scanner(System.in);
try {
System.out.println(a);
x = typ.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Please use only numbers!");
System.exit(1);
}
System.out.println(b);
String func = typ.next();
try {
System.out.println(c);
y = typ.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Please use only numbers!");
System.exit(1);
}
switch (func) {
case "+":
System.out.println(d + (x + y));
break;
case "-":
System.out.println(d + (x - y));
break;
case "/":
if (y == 0) {
System.out.println("Cannot divide by zero!");
} else {
System.out.println(d + (x/y));
}
break;
case "*":
System.out.println(d + (x * y));
break;
default:
System.out.println(func + " is not valid function");
}
}
public static void main(String[] args) {
Refresh fin = new Refresh();
fin.types();
}
}
것 "많은 구문 분석"이 질문에 –
죄송 의미 설명해주십시오을 자신의 프로그램 –