package compute01;
public class Computed {
public double computed(Double num1, Double num2,String type) {
double jieguo = 0;
switch (type) {
case "+":
jieguo = num1 + num2;
break;
case "-":
jieguo = num1 - num2;
break;
case "*":
jieguo = num1 * num2;
break;
case "/":
jieguo = num1 / num2;
break;
default:
System.out.println("输入有误!");
break;
}
return jieguo;
}
}
主方法:
package compute01;
import java.io.StreamCorruptedException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Computed a = new Computed();
String[] history = new String[100];
System.out.println("******瑞瑞牌计算器******");
while (true) {
System.out.println("请选择功能:1、进行计算2、查看记录3、关机");
int choose = sc.nextInt();
switch (choose) {
case 1:
System.out.println("欢迎使用计算器(计算功能)");
System.out.println("请输入第一个数字:");
double num1 = sc.nextDouble();
System.out.println("请输入第二个数字:");
double num2 = sc.nextDouble();
System.out.println("请输入算法:");
String type = sc.next();
if(history[history.length-1] != null) {
history = new String[100];
}
String scr = num1 + type + num2 + "=" + a.computed(num1, num2, type);
System.out.println(scr);
for (int i = 0; i < history.length; i++) {
if (history[i] == null) {
history[i] = scr;
break;
}
}
break;
case 2:
String asd = "";
for (int i = 0; i < history.length; i++) {
if (history[i] != null) {
asd += history[i] + ",";
}
}
System.out.println(asd.substring(0, asd.length() - 1));
break;
case 3:
System.exit(0);
}
}
}
}