java 实现一个简易计算器

时间:2022-12-29 15:54:20
import java.util.Scanner;
public
class Test { public static void main(String[] args) {

count();
}

public static void count() {
Scanner input1
= new Scanner(System.in);
System.out.println(
"输入一个数");
double a = input1.nextDouble();
while (true) {
Scanner input3
= new Scanner(System.in);
System.out.println(
"提示:请输入您的操作");
System.out.println(
"+");
System.out.println(
"-");
System.out.println(
"*");
System.out.println(
"/");
System.out.println(
"F:退出");
String str
= input3.nextLine();
char chr = str.toCharArray()[0];
switch (chr) {
case '+':
a
= add(a);
System.out.println(
"当前结果为:"+a);
break;
case '-':
a
= subtrace(a);
System.out.println(
"当前结果为:"+a);
break;
case '*':
a
= ride(a);
System.out.println(
"当前结果为:"+a);
break;
case '/':
a
= except(a);
System.out.println(
"当前结果为:"+a);
break;
case 'F':
System.out.println(
"结果:" + a);
System.exit(
-1);

default:
System.out.println(
"操作错误!");
break;
}
}
}

public static double add(double a) {
Scanner input2
= new Scanner(System.in);
System.out.println(
"输入一个数");
double b = input2.nextDouble();
return (a + b);
}

public static double subtrace(double a) {
Scanner input2
= new Scanner(System.in);
System.out.println(
"输入一个数");
double b = input2.nextDouble();
return (a - b);

}

public static double ride(double a) {
Scanner input2
= new Scanner(System.in);
System.out.println(
"输入一个数");
double b = input2.nextDouble();
return a * b;

}

public static double except(double a) {
Scanner input2
= new Scanner(System.in);
System.out.println(
"输入一个数");
double b = input2.nextDouble();
return a / b;

}
}