有人可以解释一下,为什么编译器在第19行把我丢给我?我只是想不通...我解决了HackerRank上的一些问题,并且我知道,有解决方法,但是我的代码可以正常工作,直到1个测试用例引发异常。而且我根本无法弄清楚,这是我阅读有关此内容的博客文章的事实。
import java.util.*;
import java.io.*;
import java.util.Scanner;
class Solution{
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
Map<String, String> contactBook = new HashMap<>();
int n = scanner.nextInt();
scanner.next();
for(int i = 0; i < n; i++) {
String name = scanner.nextLine();
String phoneNumber = scanner.nextLine();
contactBook.put(name, phoneNumber);
}
while(n-- > 0) {
String search = scanner.nextLine();
if(contactBook.containsKey(search)) {
System.out.println(search + "=" + contactBook.get(search));
} else {
System.out.println("Not found");
}
}
}
}
更改scanner.next();到scan.nextLine();如此链接中所述https://stackoverflow.com/a/24773533/7877099
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, String> contactBook = new HashMap<>();
int n = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < n; i++) {
String name = scanner.nextLine();
String phoneNumber = scanner.nextLine();
contactBook.put(name, phoneNumber);
System.out.println(name + " - " + phoneNumber);
}
while (n-- > 0) {
String search = scanner.nextLine();
if (contactBook.containsKey(search)) {
System.out.println(search + "=" + contactBook.get(search));
} else {
System.out.println("Not found");
}
}
}
}
您应使用您的代码解决以下问题:
nextLine()
代替nextInt()
和next()
。检查Scanner is skipping nextLine() after using next() or nextFoo()?了解更多信息。while
循环之外的联系人;否则,将提示用户n
次在联系人簿中输入要搜索的名称。break
循环。如果循环没有中断(即在地图中找不到名称),则n
的值最终将变为-1
,您可以使用它来打印一条消息,指出找不到该名称。下面给出的代码结合了上述要点:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 0;
boolean valid;
Map<String, String> contactBook = new HashMap<>();
do {
valid = true;
System.out.print("Enter the number of contacts to be saved: ");
try {
n = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < n; i++) {
System.out.println("---Contact#" + (i + 1) + "---");
System.out.print("Enter the name: ");
String name = scanner.nextLine();
System.out.print("Enter the phone number: ");
String phoneNumber = scanner.nextLine();
contactBook.put(name, phoneNumber);
}
} catch (NumberFormatException e) {
System.out.println("This is an invalid entry. Please try again.");
valid = false;
}
} while (!valid);
System.out.print("Enter the name to serach in the contact book: ");
String search = scanner.nextLine();
while (n-- > 0) {
if (contactBook.containsKey(search)) {
System.out.println(search + "=" + contactBook.get(search));
break;
}
}
if (n < 0) {
System.out.println("Not found");
}
}
}
示例运行:
Enter the number of contacts to be saved: 3
---Contact#1---
Enter the name: Arvind
Enter the phone number: 1234567890
---Contact#2---
Enter the name: Kumar
Enter the phone number: 1023456789
---Contact#3---
Enter the name: Avinash
Enter the phone number: 2013456789
Enter the name to serach in the contact book: Kumar
Kumar=1023456789