输入异常

 

 

 


 

1、有返回值的方法

        public int showTest(){
		Scanner in=new Scanner(System.in);		
		while(true){
			System.out.println("请输入数字");
			int num=in.nextInt();
			if(num==1){
				System.out.println("1111111111111");
				return 1;
			}else if(num==2){
				System.out.println("22222222222");
				return 2;
			}else{
				System.out.println("输入错误,重新选择");
				continue;//continue的使用				
			}
		}
	}

2、无返回值的方法 

(1)循环+continue,调用自己的方法

                while(true){
			if(choice==1){
				computer.name="刘备";
				System.out.println("你选择了刘备对战!");break;
			}else if(choice==2){
				computer.name="孙权";
				System.out.println("你选择了孙权对战!");break;
			}else if(choice==3){
				computer.name="曹操";
				System.out.println("你选择了曹操对战!");break;
			}else{
				System.out.println("输入错误请重新选择!");continue;
			}
	
		}

 (2)方法调(自己 / 其他)方法

    public void startMenu(DVDMgr act) throws ParseException{//实现菜单切换
	System.out.print("请选择:");
	Byte choice =sa.nextByte();                                                          
	switch(choice){	
	        case 1:act.add();break;
		case 2:act.search();break;
		case 6:System.out.println("谢谢使用!");break;
		default:System.out.println("输入错误,请重新输入!");this.startMenu(act);           		
            }	
    }


        public void returnMain(){//返回主菜单
		System.out.print("\n输入0返回主菜单:");
		Byte back=sa.nextByte();
		if(back==0){                                                                         			System.out.println("欢迎使用迷你DVD管理器");
			System.out.println("--------------------------------");
			System.out.println("1.新增DVD\n2.查看DVD\n3.删除DVD\n4.借出DVD\n5.归还DVD\n6.退出");
			System.out.println("--------------------------------");
		}else{
			System.out.println("输入错误,请重新输入!");
			this.returnMain();
		}
		
	}

总结:(1)while+continue—仅循环主要代码

(2)if+this.方法名()—方法调方法,循环了该方法的所有内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值