在 1 次输入后循环跳过我的扫描仪
While loop skippping my Scanners after 1 input
嘿,我正在尝试制作一个 phone 联系人项目,然后我开始创建一个 addContact()
void,它在无限循环中接受 2 个输入名称和 phone 数字。我的问题是在名称输入后没有输入 phone 数字。你能帮我么?下面是我的代码
import java.util.*;
public class Problem {
private final Scanner scanner = new Scanner(System.in);
private final ArrayList<String> contactNames = new ArrayList<>();
private final ArrayList<Integer> contactNumbers = new ArrayList<>();
private final HashMap<String, Integer> contactMap = new HashMap<>();
public void addContact() {
while (true) {
System.out.println("Enter a phone number");
int contactNumber = scanner.nextInt();
if (contactNumbers.contains(contactNumber)) {
System.out.println("This number is already given in your contacts");
break;
}
System.out.println();
System.out.println("Enter your contacts name");
String name = scanner.nextLine();
name = name.toLowerCase();
if(contactNames.contains(name)){
System.out.println("This name is already given in your contacts");
break;
}
contactNumbers.add(contactNumber);
contactNames.add(name);
contactMap.put(name, contactNumber);
System.out.println("Person is successfully added to your contacts, here is your all contacts below");
System.out.println(contactMap);
System.out.println("Do you want to quit? -> 1=yes, 2=no");
int quit = scanner.nextInt();
if(quit==1)
break;
}
}
public static void main (String[] args){
Problem a = new Problem();
a.addContact();
}
}
解决方案
scanner.nextLine(); //add this line
System.out.println();
System.out.println("Enter your contacts name");
这是为了消除用户输入 phone 号码并按下回车键后创建的换行符。
更多信息来自这里:Scanner is skipping nextLine() after using next() or nextFoo()?
嘿,我正在尝试制作一个 phone 联系人项目,然后我开始创建一个 addContact()
void,它在无限循环中接受 2 个输入名称和 phone 数字。我的问题是在名称输入后没有输入 phone 数字。你能帮我么?下面是我的代码
import java.util.*;
public class Problem {
private final Scanner scanner = new Scanner(System.in);
private final ArrayList<String> contactNames = new ArrayList<>();
private final ArrayList<Integer> contactNumbers = new ArrayList<>();
private final HashMap<String, Integer> contactMap = new HashMap<>();
public void addContact() {
while (true) {
System.out.println("Enter a phone number");
int contactNumber = scanner.nextInt();
if (contactNumbers.contains(contactNumber)) {
System.out.println("This number is already given in your contacts");
break;
}
System.out.println();
System.out.println("Enter your contacts name");
String name = scanner.nextLine();
name = name.toLowerCase();
if(contactNames.contains(name)){
System.out.println("This name is already given in your contacts");
break;
}
contactNumbers.add(contactNumber);
contactNames.add(name);
contactMap.put(name, contactNumber);
System.out.println("Person is successfully added to your contacts, here is your all contacts below");
System.out.println(contactMap);
System.out.println("Do you want to quit? -> 1=yes, 2=no");
int quit = scanner.nextInt();
if(quit==1)
break;
}
}
public static void main (String[] args){
Problem a = new Problem();
a.addContact();
}
}
解决方案
scanner.nextLine(); //add this line
System.out.println();
System.out.println("Enter your contacts name");
这是为了消除用户输入 phone 号码并按下回车键后创建的换行符。 更多信息来自这里:Scanner is skipping nextLine() after using next() or nextFoo()?