/**
我写了一半的代码,不知如何继续下去,求大佬
public class Ball {
private Color color;
private Meterial meterial;
private int weight;
}
public enum Color {
YELLOW("YELLOW","黄色"),
ORANGE("ORANGE","橙色"),
RED("RED","红色"),
BLUE("BLUE","蓝色");
}
public enum Meterial {
IRON("IRON","铁"),
WOOD("WOOD","木"),
GLASS("GLASS","玻璃");
}
public void calculate(Ball[] balls) {
int totalWeight = 0;
List<Ball> red = new ArrayList<>();
List<Ball> orange = new ArrayList<>();
List<Ball> yellow = new ArrayList<>();
List<Ball> blue = new ArrayList<>();
List<Ball> iron = new ArrayList<>();
List<Ball> wood = new ArrayList<>();
List<Ball> glass = new ArrayList<>();
HashMap<String,List<Ball>> basketsMap = new HashMap();
basketsMap.put(Color.RED.getKey(),red);
basketsMap.put(Color.ORANGE.getKey(),orange);
basketsMap.put(Color.YELLOW.getKey(),yellow);
basketsMap.put(Color.BLUE.getKey(),blue);
basketsMap.put(Meterial.IRON.getKey(),iron);
basketsMap.put(Meterial.WOOD.getKey(),wood);
basketsMap.put(Meterial.GLASS.getKey(),glass);
for (int i=0; i<balls.length; i++) {
if (basketsMap.containsKey(balls[i].getColor().getKey())) {
List<Ball> color = basketsMap.get(balls[i].getColor().getKey());
if (color.size()<3) {
color.add(balls[i]);
totalWeight+=balls[i].getWeight();
}
else {
System.out.println(basketsMap.get(balls[i].getColor().getKey()).toArray().toString());
basketsMap.remove(balls[i].getColor().getKey());
}
}
if (basketsMap.containsKey(balls[i].getMeterial().getKey())) {
List<Ball> material = basketsMap.get(balls[i].getMeterial().getKey());
if (material.size()<3) {
material.add(balls[i]);
totalWeight+=balls[i].getWeight();
}
else {
System.out.println(basketsMap.get(balls[i].getMeterial().getKey()).toArray().toString());
basketsMap.remove(balls[i].getColor().getKey());
}
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.