java扫描程序仅在检测到整数时继续
我正在使用java中的扫描器,并试图让程序仅在用户检测到整数供选择时继续,但我编写的代码没有提供该功能。这是我的密码:java扫描程序仅在检测到整数时继续,java,java.util.scanner,Java,Java.util.scanner,我正在使用java中的扫描器,并试图让程序仅在用户检测到整数供选择时继续,但我编写的代码没有提供该功能。这是我的密码: import java.util.Scanner; /** * * @author Ansel */ public class Test { public static void main(String[] args) { Scanner scan = new Scanner (System.in); AddressBook
import java.util.Scanner;
/**
*
* @author Ansel
*/
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
AddressBook ad1 = new AddressBook();
String firstName="";
String lastName="";
String key="";
String street="";
String city="";
String county="";
String postalCode="";
String mNumber="";
int choice=0;
do{
System.out.println("********************************************************************************");
System.out.println("Welcome to the Address book. Please pick from the options below.\n");
System.out.println("1.Add user \n2.Remove user \n3.Edit user \n4.List Contact \n5.Sort contacts \n6.Exit");
System.out.print("Please enter a choice: ");
int reloop = 0;
do {
try {
scan.nextLine();
choice = scan.nextInt();
reloop ++;
} catch (Exception e) {
System.out.println ("Please enter a number!");
}} while(reloop == 0);
if(choice==1){
//Add user
System.out.print("Please enter firstname: ");
firstName=scan.next();
System.out.print("Please enter lastname: ");
lastName=scan.next();
scan.nextLine();
System.out.print("Please enter street:");
street=scan.nextLine();
System.out.print("Please enter city: ");
city=scan.next();
System.out.print("Please enter county: ");
county=scan.next();
System.out.print("Please enter postal code: ");
postalCode=scan.next();
System.out.print("Please enter Mobile number: ");
mNumber=scan.next();
Address address = new Address(street,city,county,postalCode,mNumber);
key = lastName + " ".concat(firstName);
Person person = new Person(firstName,lastName,address);
ad1.addContact(key,person);
System.out.println("key: " + key);
}
else if(choice==2){
//Remove user
System.out.print("Please enter name of user to remove: ");
key=scan.nextLine();
System.out.println("name:" + key);
ad1.removeContact(key);
}
else if(choice==3){
//Edit user
}
else if(choice==4){
//List contact
System.out.println("Enter name of contact you wish to lookup: ");
key=scan.nextLine();
ad1.listContact(key);
}
else if(choice==5){
//Sort contacts
}
else{
System.out.println("Invalid choice entered, please try again");
}
}while(choice!=6);
}
}
无法正常运行的主要代码是:
int reloop = 0;
do {
try {
scan.nextLine();
choice = scan.nextInt();
reloop ++;
} catch (Exception e) {
System.out.println ("Please enter a number!");
}} while(reloop == 0);
当ran请求输入一个数字时,此代码将被删除。例如,如果您输入一个字母,它将显示一个空行,直到您输入另一个字母,然后它将显示“请输入一个数字”。我不明白为什么它没有说,只要有字母或int以外的任何东西出现,就请输入一个数字只要使用
Scanner.nextLine
和Integer.parseInt
,省去你自己的困惑
Scanner scan=新的扫描仪(System.in);
int-choice=0;
System.out.print(“请输入选项:”);
int-reloop=0;
做{
试一试{
String input=scan.nextLine();//扫描System.in中的下一行
choice=Integer.parseInt(输入);//尝试将其解析为int
reloop++;
}捕获(例外e){
System.out.println(“请输入一个数字!”);
}
}而(reloop==0);
您也可以在每次
nextLine
之后使用nextLine
终止该行,但我更喜欢像上面那样单独解析int
。它更清晰、更详细。可能重复的除了您提到的问题外,输入整数时它也不会正确继续。第二次尝试后它会继续正常工作。谢谢,这与我所做的工作有什么不同?您最初的解决方案是从输入扫描整行,不处理它,然后扫描第二行,直到找到第一个整数。此修复程序只需扫描输入中的整行,然后将其解析为整数。如果您仍然感到困惑,可能是因为nextInt
没有执行您认为的操作。查看文档。如果此答案解决了您的问题,请单击左侧的复选标记,将其标记为已接受答案。:)是的,我会的,谢谢你的帮助和解释