【发布时间】:2013-05-09 19:50:43
【问题描述】:
我的验证一直有问题,这让我很紧张,我已经用不同的循环以各种方式完成了它,我现在拥有的就是这个,如果我拿出 IF 语句如果可以很好地处理数字,但是由于我的程序只能接受从 1 到 5 的数字,所以我想采取额外的步骤来验证数字然后返回它。我尝试在循环中声明 int 选项,但不执行 return 语句。
public int makeOption(){
int option;
System.out.println("Choose from the following options: ");
System.out.println("(1)Create entry || (2)Remove entry || (3)List entry || (4)List All || (5)Exit ");
while(!reader.hasNextInt()){
System.out.println("Choose a valid option.");
reader.next();
}
if(reader.nextInt()<1 || reader.nextInt()>5){
System.out.println("Choose a valid number from options.");
reader.next();
}
option = reader.nextInt();
System.out.println("You choosed: " + option);
return option;
}
【问题讨论】:
标签: java validation input