62,614
社区成员
发帖
与我相关
我的任务
分享
boolean flag = false;
//判断输入是否匹配,不匹配就循环输入
do{
try{
DishesNum = input.nextInt();
}catch(InputMismatchException e){
System.out.println("输入不匹配,重新输入" + e);
flag =true;
}
}while(flag);
// 签收订单
private void changeState() {
boolean flag = false;
viewOrder();
Scanner input = new Scanner(System.in);
do {
int choiceNum = -1;
boolean flag2 = false;
System.out.println("请选择订单号:");
do{
try {
choiceNum = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入不匹配" + e);
flag2 =true;
}
}while(flag2);
// 如果输入的数字符合订单号
if (choiceNum > 0 && choiceNum <= orderArr.size()) {
// // 如果此时订单状态显示为未签收,则改变状态为已签收
if (!orderArr.get(choiceNum - 1).getState()) {
orderArr.get(choiceNum - 1).changeState();
System.out.println("***your order is signed!***");
} else {
System.out.println("you cant sign this order again!");
return;
}
} else {
flag = true;
}
} while (flag);
}
这是里面的一个方法,自己琢磨着写的,不知道哪出问题了。。。。[/quote]
你输入一次错误的flag是true了,然后再循环,输入对了,你也没把flag变成false,会一直循环的[/quote]
谢谢指点!// 签收订单
private void changeState() {
boolean flag = false;
viewOrder();
Scanner input = new Scanner(System.in);
do {
int choiceNum = -1;
boolean flag2 = false;
System.out.println("请选择订单号:");
do{
try {
choiceNum = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入不匹配" + e);
flag2 =true;
}
}while(flag2);
// 如果输入的数字符合订单号
if (choiceNum > 0 && choiceNum <= orderArr.size()) {
// // 如果此时订单状态显示为未签收,则改变状态为已签收
if (!orderArr.get(choiceNum - 1).getState()) {
orderArr.get(choiceNum - 1).changeState();
System.out.println("***your order is signed!***");
} else {
System.out.println("you cant sign this order again!");
return;
}
} else {
flag = true;
}
} while (flag);
}
这是里面的一个方法,自己琢磨着写的,不知道哪出问题了。。。。[/quote]
你输入一次错误的flag是true了,然后再循环,输入对了,你也没把flag变成false,会一直循环的
boolean flag = false;
//判断输入是否匹配,不匹配就循环输入
do{
flag = false;
try{
int num = input.nextInt();
}catch(Exception e){
System.out.println("输入不匹配,重新输入" + e);
flag =true;
input.next();
}
}while(flag);
// 签收订单
private void changeState() {
boolean flag = false;
viewOrder();
Scanner input = new Scanner(System.in);
do {
int choiceNum = -1;
boolean flag2 = false;
System.out.println("请选择订单号:");
do{
try {
choiceNum = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入不匹配" + e);
flag2 =true;
}
}while(flag2);
// 如果输入的数字符合订单号
if (choiceNum > 0 && choiceNum <= orderArr.size()) {
// // 如果此时订单状态显示为未签收,则改变状态为已签收
if (!orderArr.get(choiceNum - 1).getState()) {
orderArr.get(choiceNum - 1).changeState();
System.out.println("***your order is signed!***");
} else {
System.out.println("you cant sign this order again!");
return;
}
} else {
flag = true;
}
} while (flag);
}
这是里面的一个方法,自己琢磨着写的,不知道哪出问题了。。。。 //判断输入是否匹配,不匹配就循环输入
do{
try{
DishesNum = input.nextInt();
break;
}catch(InputMismatchException e){
System.out.println("输入不匹配,重新输入" + e);
continue;
}
}while(true);