public static void main(String[] args) {
Drive drive = new Drive();
drive.operation();
}
}
package 测试;
import 内核.Kernel;
import 内核.Second;
import 内核.first;
public class Calculate extends Kernel implements first,Second{
public Calculate() {
// TODO Auto-generated constructor stub
}
public String sound(String string) {
if (string.equals("高级")) {
return "有声音!";
}return "没有声音!";
}
public double add(double a,double b) {
return a + b;
}
public double sub(double a,double b) {
return a - b;
}
public double mul(double a,double b) {
return a * b;
}
public double div(double a,double b) {
return a / b;
}
public double mod(double a,double b) {
return a % b;
}
public double levelFactorial(double n) {
int j = (int)n;
int sum = 1;
if (j < 0) {
throw new IllegalArgumentException("必须为正数!");
}
for (int i = 1;i <= j;i++) {
sum *= i;
}return sum;
}
public double level2Factorial(double n) {
double j = (double)n;
int sum = 1;
if (j < 0) {
throw new IllegalArgumentException("必须为正数!");
}
for (int i = 1;i <= j;i++) {
sum *= i;
}
int sum1 = 1;
for (int k = 1;k <= sum;k++) {
sum1 *= k;
}return sum1;
}
}
package 内核;
public abstract class Kernel {
public String name;
public abstract String sound(String string);
public abstract double add(double a,double b);
public abstract double sub(double a,double b);
public abstract double mul(double a,double b);
public abstract double div(double a,double b);
public abstract double mod(double a,double b);
}
package 驱动;
import java.util.Scanner;
import 测试.Calculate;
public class Drive {
public void operation() {
System.out.println("请选择计算器的级别:(高级/普通)");
Calculate c = new Calculate();
Scanner s1 = new Scanner(System.in);
String str = s1.nextLine();
System.out.println(c.sound(str));
Scanner s5 = new Scanner(System.in);
System.out.println("请选择要进行的运算:(基本运算/阶乘运算)");
String str3 = s5.nextLine();
boolean sign = true;
while (true) {
if (str3.equals("基本运算")) {
boolean mmm = true;
double a = 1;
while (mmm) {
Scanner s2 = new Scanner(System.in);
System.out.println("请输入第一个数:");
String one = s2.nextLine();
if (one == null || str.equals("")) {
System.out.println("输入不合法!");
mmm = true;
continue;
}
if (one.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")){
a = Double.parseDouble(one);
mmm = false;
}else {
System.out.println("输入不合法!");
}
}
boolean nnn = true;
double b = 1;
while (nnn) {
Scanner s3 = new Scanner(System.in);
System.out.println("请输入第二个数:");
String one = s3.nextLine();
if (one == null || str.equals("")) {
System.out.println("输入不合法!");
nnn = true;
continue;
}
if (one.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")){
b = Double.parseDouble(one);
nnn = false;
}else {
System.out.println("输入不合法!");
}
}
Scanner s4 = new Scanner(System.in);
System.out.println("请输入运算符号:");
String str2 = s4.nextLine();
switch (str2) {
case "+":
System.out.println(c.add(a, b));
break;
case "-":
System.out.println(c.sub(a, b));
break;
case "*":
System.out.println(c.mul(a, b));
break;
case "/":
System.out.println(c.div(a, b));
break;
case "%":
System.out.println(c.mod(a, b));
break;
default:System.out.println("不好意思,给不了更多帮助啦!");
}
}else if (str3.equals("阶乘运算")) {
if (str.equals("普通")) {
System.out.println("你在逗我吧!");
return;
}
System.out.println("请选择:(一阶/二阶)");
Scanner sc1 = new Scanner(System.in);
String str4 = sc1.nextLine();
if (str4.equals("一阶")) {
System.out.println("请输入一个数:");
Scanner sc2 = new Scanner(System.in);
double m = sc2.nextDouble();
System.out.println(c.levelFactorial(m));
}else {
System.out.println("请输入一个数:");
Scanner sc3 = new Scanner(System.in);
double m = sc3.nextDouble();
System.out.println(c.level2Factorial(m));
}
}
sign = false;
System.out.println("是否继续运算? (是/否)");
Scanner xx = new Scanner(System.in);
String zz = xx.nextLine();
if (zz.equals("是")) {
sign = true;
}else {
System.out.println("感谢使用!");
System.exit(0);
}
}
}
}