私はJavaプログラミングの初心者です。電卓プログラムを作成しましたが、うまくいくと思われますが、他のプログラマーは電卓プログラムで「多くの構文解析」を使用するようです。私は間違ったアプローチをとっているのか、この種のロジックを使って将来的に問題に遭遇するのかと尋ねたがっています。コードjavaの効率に関する助言が必要です
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();
}
}
「多くのことを解析すると、」この質問 –
申し訳ありませんが、私は自分の中で「パース」の方法をたくさん見る意味では何を意味するかを説明してくださいプログラム –