package coins;
import java.util.HashMap;
import java.util.Scanner;
public class Coin {
private HashMap<Integer,String> coinnames = new HashMap<Integer,String>();
public Coin(){
coinnames.put(1,"penny");
coinnames.put(10,"dime");
coinnames.put(25,"quarter");
coinnames.put(50,"half_dolar");
coinnames.put(50, "五毛");
System.out.println(coinnames.keySet().size());//得到HashMap数量
System.out.println(coinnames);//打印HashMap
for(Integer k : coinnames.keySet() ) {
String s = coinnames.get(k);
System.out.println(s);
}
}
public String getName(int amount){
if(coinnames.containsKey(amount))
return coinnames.get(amount);
else
return "Not Found";
}
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int amount = in.nextInt();
Coin coin = new Coin();
String name = coin.getName(amount);
System.out.println(name);
}
}
新加进去的同名东西会覆盖掉以前的东西,用foreach可以遍历里面每一个数据
HashMap 一对一对的值,一个索引,一个值