如何为整数分配重要性级别?

时间:2017-02-21 07:20:41

标签: java class methods constructor

我必须制定一个程序,允许沙鼠开采四种金属,这四种金属具有不同的价值水平。沙鼠一次只能携带10盎司。沙鼠将优先携带更高价值的金属。我只是开始类,方法和构造函数,所以我正在做的代码不能有任何太高级的东西。有帮助吗?这是我到目前为止所拥有的。

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);
}

}

1 个答案:

答案 0 :(得分:0)

你可以尝试这样:

...
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
}