我希望能够使用Scanner
来让用户输入哈希值来检索信息。
Hashtable<Integer, String> hash =
new Hashtable<Integer, String>();
hash.put(0 , "BabyS");
hash.put(1 , "BabyA");
hash.put(2 , "BabyN");
hash.put(3 , "BabyE");
Scanner i = new Scanner(System.in);
int Hv = i.nextInt();
System.out.println("Get the information for ID#3: "+ hash.get(Hv));
答案 0 :(得分:2)
我可以使您的确切代码段起作用,但是我认为您可能会感到困惑,因为Scanner
不会提示您输入。诸如此类的东西对用户来说是一点点信息,但是请在对问题的评论中采纳GhostCat的建议。
import java.util.HashMap;
import java.util.Scanner;
public class j {
public static void main(String[] args) {
HashMap<Integer, String> hash = new HashMap<>();
hash.put(1, "BabyS");
hash.put(2, "BabyA");
hash.put(3, "BabyN");
hash.put(4, "BabyE");
System.out.printf("Please input a number between 1 and %s: ", hash.size());
Scanner i = new Scanner(System.in);
int Hv = i.nextInt();
System.out.printf("Get the information for ID#%s: ", Hv);
System.out.println(hash.get(Hv));
}
}