使用java语言写出一个可以进行连加连除的计算器代码
时间: 2023-06-03 12:06:03 浏览: 104
好的,以下是可以进行连加连除的计算器代码:
```
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double result = 0;
System.out.print("请输入第一个数字:");
double num1 = scanner.nextDouble();
result = num1;
while (true) {
System.out.print("请输入操作符:(+, -, *, /, q退出) ");
String operator = scanner.next();
if ("+".equals(operator)) {
System.out.print("请输入要加的数字:");
double num = scanner.nextDouble();
result += num;
System.out.println("结果是:" + result);
} else if ("-".equals(operator)) {
System.out.print("请输入要减的数字:");
double num = scanner.nextDouble();
result -= num;
System.out.println("结果是:" + result);
} else if ("*".equals(operator)) {
System.out.print("请输入要乘的数字:");
double num = scanner.nextDouble();
result *= num;
System.out.println("结果是:" + result);
} else if ("/".equals(operator)) {
System.out.print("请输入要除的数字:");
double num = scanner.nextDouble();
if (num == 0) {
System.out.println("除数不能为0");
continue;
} else {
result /= num;
System.out.println("结果是:" + result);
}
} else if ("q".equals(operator)) {
break;
} else {
System.out.println("操作符不正确,请重新输入。");
}
}
System.out.println("感谢使用!");
}
}
```
阅读全文