这里是主程序从分配相关的部分:写作计算器类为“命令行计算器”程序(JAVA)
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);
}
}
}
我只允许写如下所示的类(计算器)分配到作业中的程序中。我无法弄清楚如何从计算中获得可打印的结果。
我认为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);
}
}
你会得到结果值int结果变量? –