割り当てからのメインプログラムの関連部分は次のとおりです。
public static void main(String [] args) {
int characters = args.length;
if (characters < 3)
System.out.println("You did not type in a calculation!");
else if (characters % 2 == 0)
System.out.println("Invalid number of command line parameters.");
else {
Calculathor counter = new Calculathor();
counter.count(args);
}
}
}
課題のプログラムには、以下に示すクラス (Calculator) のみを記述できます。計算から印刷可能な結果を得る方法がわかりません。
switch ステートメントは、ユーザーの入力から演算子を受け取り、それを使用する際に意図したとおりに機能していると思いますが、実際の結果を得るには別のものが必要です。
class Calculator {
int result;
int i = 0;
String[] args;
void count(String[] args) {
switch (args[i].charAt(i)) {
case '+': result = Integer.parseInt(args[i]) +
Integer.parseInt(args[i+2]);
break;
case '-': result = Integer.parseInt(args[i]) -
Integer.parseInt(args[i+2]);
break;
}
System.out.println("\nResult of the calculation " + args[0] + " + " + args[2] + " + " + args[4] + " - " + args[6] + " is " + result);
}
}