import java.util.HashMap; import java.util.Scanner;
公共类FIftyStates {public static void main(String[] args) {
HashMap<Integer, String> FiftyStates = new HashMap <Integer, String>();
//ad values in the map
Scanner scan = new Scanner(System.in);
System.out.Println(FiftyStates.get(scan.nextInt()));
FiftyStates.put(1, "Delaware");
FiftyStates.put(2,"Pennsylvania");
FiftyStates.put(3,"New Jersey");
FiftyStates.put(4,"Georgia");
FiftyStates.put(5,"Connecticut");
FiftyStates.put(6,"Massachusetts" );
FiftyStates.put(7,"Maryland");
FiftyStates.put(8,"South Carolina");
FiftyStates.put(9,"New Hampshire");
FiftyStates.put(10,"Virginia");
FiftyStates.put(11,"New York");
FiftyStates.put(12,"North Carolina");
FiftyStates.put(13,"Rhode Island");
FiftyStates.put(14,"Vermont");
FiftyStates.put(15,"Kentucky");
FiftyStates.put(16,"Tennessee");
FiftyStates.put(17,"Ohio");
FiftyStates.put(18,"Louisiana");
FiftyStates.put(19,"Indiana");
FiftyStates.put(20,"Mississippi");
FiftyStates.put(21,"Illinois");
FiftyStates.put(22,"Alabama");
FiftyStates.put(23,"Maine");
FiftyStates.put(24,"Missouri");
FiftyStates.put(25,"Arkansas");
FiftyStates.put(26,"Michagan");
FiftyStates.put(27,"Florida");
FiftyStates.put(28,"Texas");
FiftyStates.put(29,"Iowa");
FiftyStates.put(30,"Wisconsin");
FiftyStates.put(31,"California");
FiftyStates.put(32,"Minnesota");
FiftyStates.put(33,"Oregon");
FiftyStates.put(34,"Kansas");
FiftyStates.put(35,"West Virginia");
FiftyStates.put(36,"Nevada");
FiftyStates.put(37,"Nebraska");
FiftyStates.put(38,"Colorado");
FiftyStates.put(39,"North Dakota");
FiftyStates.put(40,"South Dakota");
FiftyStates.put(41,"Montana");
FiftyStates.put(42,"Washington");
FiftyStates.put(43,"Idaho");
FiftyStates.put(44,"Wyoming");
FiftyStates.put(45,"Utah");
FiftyStates.put(46,"Oklahoma");
FiftyStates.put(47,"New Mexico");
FiftyStates.put(48,"Arizona");
FiftyStates.put(49,"Alaska");
FiftyStates.put(50,"Hawaii");
}
}
答案 0 :(得分:1)
您可以通过反转地图来实现您想要的效果。 请参阅下面的代码。
HashMap<Integer, String> FiftyStates = new HashMap <Integer, String>();
FiftyStates.put(1,"Delaware");
FiftyStates.put(2,"Pennsylvania");
...
...
...
...
FiftyStates.put(49,"Alaska");
FiftyStates.put(50,"Hawaii");
Scanner scan = new Scanner(System.in);
System.out.Println(FiftyStates.get(scan.nextInt()));
不要忘记关闭扫描仪。