[Java] 纯文本查看 复制代码
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Map<String,Double> map = new HashMap<String,Double>();
map.put("茄子", 2.0);
map.put("黄瓜", 2.5);
map.put("西红柿", 5.0);
map.put("土豆", 3.0);
map.put("豆角", 3.5);
Scanner sc = new Scanner(System.in);
String input = sc.next();
Double num = map.get(input);
if(num == null){
System.out.println("已经卖完");
}else{
System.out.println(num);
}
}
}
[Java] 纯文本查看 复制代码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashMap<String, Double> verg = new HashMap<>();
verg.put("茄子", 2.0);
verg.put("黄瓜", 2.5);
verg.put("西红柿", 5.0);
verg.put("土豆", 3.0);
verg.put("豆角", 3.5);
System.out.println("请输入蔬菜名称:");
String line = sc.nextLine();
// 查询蔬菜价格
checkPrice(verg,line);
String item = "黄瓜";
verg.put(item, verg.get(item) + 1.0);
System.out.println("--------------------------------------------");
System.out.println("打印集合:");
System.out.println(verg);
System.out.println("--------------------------------------------");
// 价格大于3的蔬菜打印出来
System.out.println("价格大于3.0的蔬菜有:");
Set<String> vergName = verg.keySet();
for (String name : vergName) {
if(verg.get(name) > 3.0) {
System.out.println(name);
}
}
}
private static void checkPrice(HashMap<String, Double> verg, String name) {
Set<String> vergName = verg.keySet();
if(vergName.contains(name)) {
System.out.println(name + ":" + verg.get(name));
} else {
System.out.println(name + "已经卖完!");
}
}