java加减乘除的运算,这个要怎么改,用键盘输入需要计算的数字。。。萌新。。。
发布网友
发布时间:2022-04-26 20:22
我来回答
共1个回答
热心网友
时间:2023-10-28 22:34
//刚写的参考下吧,感觉两个问题都是你一个人提的!不足之处还请指点!
import java.util.Scanner;
public class 计算器 {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
init();
}
private static void init() {
while (true)
sop(input());
}
private static String input() {
sop("请输入运算表达式如:1+1回车!");
String str = sc.nextLine(), sum = "";
char tem = 0;
int poin = 0;
for (int i = 0; i < str.length(); i++) {//遍历!
char cs = str.charAt(i);
if (cs == '+' || cs == '-' || cs == '*' || cs == '/') {// 寻找找运算符号
tem = str.charAt(i);// 记录运算符号!
poin = i;// 记录符号位置!
break;
} else if (i >= str.length() - 1) {
sop("输入不合法请检查!");
init();
}
}// 拆分数字!
double a = Double.parseDouble(str.substring(0, poin));
double b = Double.parseDouble(str.substring(poin + 1));
switch (tem) {// 开始判断运算!
case '+':
sum = "" + (a + b);
break;
case '-':
sum = "" + (a - b);
break;
case '*':
sum = "" + (a * b);
break;
default:
sum = "" + (a / b);
break;
}
return str + "=" + sum;
}//打印功能!
private static void sop(Object obj) {
System.out.println(obj);
}
}
热心网友
时间:2023-10-28 22:34
//刚写的参考下吧,感觉两个问题都是你一个人提的!不足之处还请指点!
import java.util.Scanner;
public class 计算器 {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
init();
}
private static void init() {
while (true)
sop(input());
}
private static String input() {
sop("请输入运算表达式如:1+1回车!");
String str = sc.nextLine(), sum = "";
char tem = 0;
int poin = 0;
for (int i = 0; i < str.length(); i++) {//遍历!
char cs = str.charAt(i);
if (cs == '+' || cs == '-' || cs == '*' || cs == '/') {// 寻找找运算符号
tem = str.charAt(i);// 记录运算符号!
poin = i;// 记录符号位置!
break;
} else if (i >= str.length() - 1) {
sop("输入不合法请检查!");
init();
}
}// 拆分数字!
double a = Double.parseDouble(str.substring(0, poin));
double b = Double.parseDouble(str.substring(poin + 1));
switch (tem) {// 开始判断运算!
case '+':
sum = "" + (a + b);
break;
case '-':
sum = "" + (a - b);
break;
case '*':
sum = "" + (a * b);
break;
default:
sum = "" + (a / b);
break;
}
return str + "=" + sum;
}//打印功能!
private static void sop(Object obj) {
System.out.println(obj);
}
}
热心网友
时间:2023-10-28 22:34
//刚写的参考下吧,感觉两个问题都是你一个人提的!不足之处还请指点!
import java.util.Scanner;
public class 计算器 {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
init();
}
private static void init() {
while (true)
sop(input());
}
private static String input() {
sop("请输入运算表达式如:1+1回车!");
String str = sc.nextLine(), sum = "";
char tem = 0;
int poin = 0;
for (int i = 0; i < str.length(); i++) {//遍历!
char cs = str.charAt(i);
if (cs == '+' || cs == '-' || cs == '*' || cs == '/') {// 寻找找运算符号
tem = str.charAt(i);// 记录运算符号!
poin = i;// 记录符号位置!
break;
} else if (i >= str.length() - 1) {
sop("输入不合法请检查!");
init();
}
}// 拆分数字!
double a = Double.parseDouble(str.substring(0, poin));
double b = Double.parseDouble(str.substring(poin + 1));
switch (tem) {// 开始判断运算!
case '+':
sum = "" + (a + b);
break;
case '-':
sum = "" + (a - b);
break;
case '*':
sum = "" + (a * b);
break;
default:
sum = "" + (a / b);
break;
}
return str + "=" + sum;
}//打印功能!
private static void sop(Object obj) {
System.out.println(obj);
}
}