2016-12-05 9 views
0

文字列のUnicode値を取得し、10%を掛けて現在のオブジェクトのレベルを追加するように求められています。それは、私はまだコードを含むロジックがダウンしていることが判明したので、イライラしています:まだ:予期:< 0>があったエラー:< 8>を取得します。どんな提案も、多分、私はロジックにしなければならないわずかなニュアンスですが、私はそれが正しいと確信しています。 getLevelメソッドのをメモエラーが文字列内の最初の文字のユニコード値を取得する

public class PouchCreature implements Battleable { 

private String name; 
private int strength; 
private int levelUps; 
private int victoriesSinceLevelUp; 

/** 
* Standard constructor. levelUps and victoriesSinceLevelUp start at 0. 
* 
* @param nameIn desired name for this PouchCreature 
* @param strengthIn starting strength for this PouchCreature 
*/ 
public PouchCreature(String nameIn, int strengthIn) { 
    this.name = nameIn; 
    this.strength = strengthIn; 
    this.levelUps = 0; 
    this.victoriesSinceLevelUp = 0; 
} 


/** 
* Copy constructor. 
* 
* @param other reference to the existing object which is the basis of the new one 
*/ 
public PouchCreature(PouchCreature other) { 
    this.name=other.name; 
    this.strength=other.strength; 
    this.levelUps=other.levelUps; 
    this.victoriesSinceLevelUp=other.victoriesSinceLevelUp; 
} 


/** 
* Getter for skill level of the PouchCreature, which is based on the 
* first character of its name and the number of levelUps it has. 
* Specifically, the UNICODE value of the first character in its name 
* taken %10 plus the levelUps. 
* 
* @return skill level of the PouchCreature 
*/ 
public int getLevel() { 
    int value = (int)((int)(getName().charAt(0)) * 0.1); 
    return value + this.levelUps; 
} 

答えて

0

ここでそれだからあなたが増加 10%の値になっていると述べました。あなたが実際にやっているのは、です。そのうち10%を取ることでを90%減らします(そしてそれをintに切り捨てます)。 67.0 * 0.1 = 6.7であり、intに切り捨てられた場合、6である。 10%

変更0.11.1に増加て:

あり
int value = (int)((int)(getName().charAt(0)) * 1.1); 
// --------------------------------------------^ 

(例えば)getName()戻り"Centaur"場合、Cは、Unicode値67を有し、value73なってしまいます。

+0

私の間違いは、実際にはモジュラスを取るように言ったが、ありがとう! – yarcenahs

0

クラスを呼び出しているコードを参照する必要があり、エラーメッセージが生成されています。なぜ0を期待しているのですか? 8あなたが与えた情報から有効な戻り値のように思えます。

関連する問題