先来看一下<<面向对象编程>>这本教材第一章第16个练习给的代码:
package chapter2.e16;
import java.util.Scanner;
public class TwoLoopTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
menu();
play();
}
private static void menu() {
// TODO Auto-generated method stub
System.out.println("**********欢迎使用计算器**********");
System.out.println("1.加法运算");
System.out.println("2.减法运算");
System.out.println("3.乘法运算");
System.out.println("4.除法运算");
System.out.println("5.取余运算");
System.out.println("6.退出");
System.out.println("*******************************");
}
private static void play() {
// TODO Auto-generated method stub
int i = 0;
double a = 0.0;
double b = 0.0;
Scanner sc = new Scanner(System.in);
System.out.println("输入您要进行的操作:");
i = sc.nextInt();
while (true) {
switch (i) {
case 1:
System.out.println("输入两个数进行加法");
a = sc.nextDouble();
b = sc.nextDouble();
System.out.println("计算结果为:" + (a + b));
break;
case 2:
System.out.println("输入两个数进行减法");
a = sc.nextDouble();
b = sc.nextDouble();
System.out.println("计算结果为:" + (a - b));
break;
case 3:
System.out.println("输入两个数进行乘法");
a = sc.nextDouble();
b = sc.nextDouble();
System.out.println("计算结果为:" + (a * b));
break;
case 4:
System.out.println("输入两个数进行除法");
a = sc.nextDouble();
b = sc.nextDouble();
System.out.println("计算结果为:" + (a / b));
break;
case 5:
System.out.println("输入两个数进行取余");
a = sc.nextDouble();
b = sc.nextDouble();
System.out.println("计算结果为:" + (a % b));
break;
case 6:
System.out.println("*********谢谢使用!*********");
break;
default:
System.out.println("输入有误!");
}
menu();
play();
}
}
}
简单地看好像没啥问题,我们运行一下:
嗯?居然报错了?!看一下代码:
原来是要一个数一个数地输入,好吧,那我先退出程序:
居然无法退出?!原来是case 6没有break!但是,你以为把break加上就可以了吗?
no!一个break只是跳出了case语句,不能跳出while循环!
HOW TO DO?
- 增加输入判断的函数,输入不是整数则打回
- case语句重复内容过多,把他们提出来!
- 一次性退出while循环!
增加输入判断的函数,输入不是整数则打回
原程序中,如果输入字母或特殊字符时,会报错:
来看看输入语句:
Scanner sc = new Scanner(System.in);
i = sc.nextInt();
很明显,nextInt()只能接受整型,类型不对自然会报错,怎么解决?
加一个输入判断的函数:
private static int judgeInput() {
int number = 0;
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt()) {
number = sc.nextInt();
}
else {
System.out.println("Please check and input again");
menu();
}
return number;
}
如果输入不是整数,则重新输入;输入正确后直接return刚刚输入的值
case语句重复内容过多,把他们提出来!
让我们把这些重复的语句全部提出来!
System.out.println("Please enter the first number:");
a = sc.nextDouble();
System.out.println("The second number:");
b = sc.nextDouble();
这里顺手加上提示语句,好让用户明白是输入两次
但是case 6不需要输入,所以这里我们加一句判断:
if (i != 6) {
System.out.println("Please enter the first number:");
a = sc.nextDouble();
System.out.println("The second number:");
b = sc.nextDouble();
}
不然退出之前还要输入两个数
一次性退出while循环!
这时,我们给while循环加上一个标记:
circulatory:
while (true){
switch (i) {
case 6:
System.out.println("*********谢谢使用!*********");
break circulatory;
}
}
这样就可以直接跳出while循环了
其实这些东西都很细节,但是往往越是细节的地方,就越是要注意
下面是我修改好的代码,其中还有很多可以修改的地方,欢迎大家批评指正:
package project1;
import java.util.Scanner;
/**
* @author 郑博培
*/
public class CalculatorDemo {
public static void main(String[] args){
menu();
}
private static void menu(){
System.out.println("**********Calculator Menu**********");
System.out.println("1.加法运算");
System.out.println("2.减法运算");
System.out.println("3.乘法运算");
System.out.println("4.除法运算");
System.out.println("5.取余运算");
System.out.println("6.退出");
System.out.println("*******************************");
System.out.println("输入您要进行的操作:");
play(judgeInput());
}
private static int judgeInput() {
int number = 0;
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt()) {
number = sc.nextInt();
} else {
System.out.println("Please check and input again");
menu();
}
return number;
}
private static void play(int i) {
double a = 0.0;
double b = 0.0;
Scanner sc = new Scanner(System.in);
circulatory:
while (true) {
if (i != 6) {
System.out.println("Please enter the first number:");
a = sc.nextDouble();
System.out.println("The second number:");
b = sc.nextDouble();
}
switch (i) {
case 1:
System.out.println("两个数进行加法");
System.out.println("计算结果为:" + (a + b));
break;
case 2:
System.out.println("两个数进行减法");
System.out.println("计算结果为:" + (a - b));
break;
case 3:
System.out.println("两个数进行乘法");
System.out.println("计算结果为:" + (a * b));
break;
case 4:
System.out.println("两个数进行除法");
System.out.println("计算结果为:" + (a / b));
break;
case 5:
System.out.println("两个数进行取余");
System.out.println("计算结果为:" + (a % b));
break;
case 6:
System.out.println("*********谢谢使用!*********");
break circulatory;
default:
System.out.println("输入有误!");
}
menu();
}
}
}