Java 如何为INT分配重要性级别?
我必须制定一个程序,允许沙鼠去开采四种金属,这四种金属有不同的价值水平。沙鼠一次只能携带10盎司。沙鼠会优先携带价值更高的金属。我刚刚开始学习类、方法和构造函数,所以我正在编写的代码不能太高级。有什么帮助吗?这是我到目前为止所拥有的Java 如何为INT分配重要性级别?,java,class,methods,constructor,Java,Class,Methods,Constructor,我必须制定一个程序,允许沙鼠去开采四种金属,这四种金属有不同的价值水平。沙鼠一次只能携带10盎司。沙鼠会优先携带价值更高的金属。我刚刚开始学习类、方法和构造函数,所以我正在编写的代码不能太高级。有什么帮助吗?这是我到目前为止所拥有的 public class Gerbil { private int totalRhodium; private int totalPlatinum; private int totalGold; private int totalIron; private int
public class Gerbil {
private int totalRhodium;
private int totalPlatinum;
private int totalGold;
private int totalIron;
private int totals;
private int limit=10;
public Gerbil() {
}
public int printTotals() {
totals=totalRhodium+totalPlatinum+totalGold+totalIron;
return totals;
}
public void goMining(int rhodium, int platinum, int gold, int iron) {
System.out.println("Rhodium: "+rhodium);
System.out.println("Platinum: "+platinum);
System.out.println("Gold: "+gold);
System.out.println("Iron: "+iron);
}
}您可以这样尝试:
...
int totals = 0;
int totalsRhodium = 0;
int totalsPlatinum = 0;
int totalsGold = 0;
int totalsIron = 0;
int limit = 10;
public void goMining(int rhodium, int platinum, int gold, int iron) {
if(rhodium >= limit) {
this.totalsRhodium = limit;
return;
} else {
this.totalsRhodium = rhodium;
limit = limit - rhodium;
}
if(platinum >= limit) {
this.totalsPlatinum = limit;
return;
} else {
this.totalsPlatinum = platinum;
limit = limit - platinum;
}
//go on with the other metals
}
您将需要使用循环来实现此功能。您可以使用hashmap,其中键是优先级,值是金属itself@Akshay哦,我想我需要…我想我会用Goming方法。我只是不知道如何准确地计算程序将知道一种金属是否比另一种重要。bump!我真的需要帮助:(试一下if..else-else-if循环…玩一点arround,否则你就学不会了