四则运算代码java 四则运算java编程( 五 )


}
public static void calc() {
Scanner reader = new Scanner(System.in);
char op;
double num1, num2, result;
while (true) {
System.out.println("请选择运算符:1、+;2、-;3、*;4、/");
if (reader.hasNext()) {
op = reader.nextChar();
switch (op) {
case '1':
op = 1;
break;
case '2':
op = 2;
break;
case '3':
op = 3;
break;
case '4':
op = 4;
break;
case 0://EOF
default:
System.out.println("输入非法!");
op = 0;
break;
}
if (0 != op) {
try {
System.out.println("请输入第一个运算数:");
if (reader.hasNext()) {
num1 = reader.nextDouble();
}
System.out.println("请输入第二个运算数:");
if (reader.hasNext()) {
num2 = reader.nextDouble();
}
switch (op) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
}
System.out.println("运算结果为:"+ result);
System.out.println("是否继续?是(Y)/否(N):");
} catch(Exception err) {
System.out.println("输入非法!");
}
if (!isContinue(reader)) {
break;
}
}
}
}
}
public static boolean isContinue(Scanner reader) {
boolean flag = false;
if (reader.hasNext()) {
char isContinue = reader.next().charAt(0);
if ('N' == isContinue) {
flag = false;
} else if ('Y' == isContinue) {
flag = true;
} else {
System.out.println("是否继续?是(Y)/否(N):");
flag = isContinue(reader);
}
}
return flag;
}
}
关于四则运算代码java和四则运算java编程的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

推荐阅读