Java ⼩学算术10以内的加减乘除运算
package demo;
import java.util.Scanner;
public class Test {
static Scanner sc = new Scanner(System.in);
public static boolean isNum(String str) {
boolean a = str.matches("?(([0-9]+)()?|()?)");return a && !b;
}
public static double input(int i) {
System.out.print(“输⼊第” + i + “个数:”);
String num = “”;
do {
num = sc.next();
if (!isNum(num)) {
System.out.print(“输⼊的不为数字,请重新输⼊:”);
}
} while (!isNum(num));
return Double.parseDouble(num);
}
public static String input2() {
String operator = “”;
boolean re;
System.out.print(“输⼊运算符(+、-、、/、^):");
do {
operator = sc.next();
re = operator.equals("+") || operator.equals("-") || operator.equals("”) || operator.equals("/")|| operator.equals("^");
if (!re) {
System.out.print(“请输⼊(+、-、*、/、^)中的⼀个:”);
}
} while (!re);
java switch case stringreturn operator;
}
");booleanb =str .matches ("−[+]?
public static void main(String[] args) {
String judge = “”;
do {
double n1 = input(1);
double n2 = input(2);
String result = “”;
double res = 0;
String op = null;
op = input2();
switch (op) {
case “+”:
res = n1 + n2;
break;
case “-”:
res = n1 - n2;
break;
case “*”:
res = n1 * n2;
break;
case “/”:
if (n2 == 0) {
result = “除数不能为0”;
} else {
res = n1 / n2;
}
break;
case “^”:
res = Math.pow(n1, n2);
break;
}
if (result.isEmpty()) {
result = String.valueOf(res);
}
System.out.println(“计算结果:” + n1 + op + n2 + “=” + result); System.out.print(“是否继续?(Y/任意退出)”);
judge = sc.next();
} while (judge.equals(“Y”) || judge.equals(“y”));
System.out.println(“谢谢使⽤_(:зゝ∠)_”);
}
1. -+

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。