小白求大神指点,控制台输入问题

sinat_34573100 2016-09-14 06:38:49
boolean flag = false;
//判断输入是否匹配,不匹配就循环输入
do{
try{
DishesNum = input.nextInt();
}catch(InputMismatchException e){
System.out.println("输入不匹配,重新输入" + e);
flag =true;
}
}while(flag);

我想实现如果输入的不是int类型的值就报错然后跳转到前面循环输入,但是这样试了一下不行啊,求大神指点一下应该怎么做
...全文
134 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinat_34573100 2016-09-15
  • 打赏
  • 举报
回复
引用 6 楼 dzq584462393 的回复:
[quote=引用 4 楼 sinat_34573100 的回复:] [quote=引用 3 楼 NewMoons 的回复:] 请贴上相对完整的代码。
// 签收订单
	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] 谢谢指点!
sinat_34573100 2016-09-15
  • 打赏
  • 举报
回复
引用 5 楼 Molly_1994 的回复:
因为它一直在读取错误的那个字符,你加个input.next();

boolean flag = false;
            //判断输入是否匹配,不匹配就循环输入
            do{
				flag = false;
                try{
                    int num = input.nextInt();
					
                }catch(Exception e){
                    System.out.println("输入不匹配,重新输入" + e);
                    flag =true;
		    input.next();
                }
            }while(flag);
谢谢!我试试
dzq584462393 2016-09-15
  • 打赏
  • 举报
回复
引用 4 楼 sinat_34573100 的回复:
[quote=引用 3 楼 NewMoons 的回复:] 请贴上相对完整的代码。
// 签收订单
	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,会一直循环的
anakin_feng 2016-09-14
  • 打赏
  • 举报
回复
因为它一直在读取错误的那个字符,你加个input.next();

boolean flag = false;
            //判断输入是否匹配,不匹配就循环输入
            do{
				flag = false;
                try{
                    int num = input.nextInt();
					
                }catch(Exception e){
                    System.out.println("输入不匹配,重新输入" + e);
                    flag =true;
		    input.next();
                }
            }while(flag);
sinat_34573100 2016-09-14
  • 打赏
  • 举报
回复
引用 3 楼 NewMoons 的回复:
请贴上相对完整的代码。
// 签收订单
	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);
	}
这是里面的一个方法,自己琢磨着写的,不知道哪出问题了。。。。
NewMoons 2016-09-14
  • 打赏
  • 举报
回复
请贴上相对完整的代码。
sinat_34573100 2016-09-14
  • 打赏
  • 举报
回复
引用 1 楼 abcdefghiijklmnopqrs 的回复:
 //判断输入是否匹配,不匹配就循环输入
do{
try{
DishesNum = input.nextInt();
break;
}catch(InputMismatchException e){
System.out.println("输入不匹配,重新输入" + e);
continue;
}
}while(true);

不行啊,这个和我那个是一样的吧,试了一下,结果
  • 打赏
  • 举报
回复
 //判断输入是否匹配,不匹配就循环输入
            do{
                try{
                    DishesNum = input.nextInt();        
                    break;
                }catch(InputMismatchException e){
                    System.out.println("输入不匹配,重新输入" + e);
                   continue;
                }
            }while(true);

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧