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.方法名()—方法调方法,循环了该方法的所有内容