2016-12-05 155 views
0

我基本上被要求獲取字符串的Unicode值,將其乘以10%並添加對象當前所具有的任何級別。這是令人沮喪的,因爲事實證明,我已經包括代碼的邏輯,但我仍然得到一個錯誤,說:期望:< 0>但是:< 8>。任何建議,也許這只是我必須在邏輯中做出的細微差別,儘管我相當確定它是正確的。 。注意到getLevel方法的,因爲那是錯誤的獲取字符串中第一個字符的Unicode值

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%的價值。但是,實際上你在做的是減少 90%,只取其中的10%(然後將其截斷爲int)。 67.0 * 0.1 = 6.7,當截斷爲int時爲6

更改0.11.1增加它由10%:

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

在那裏,如果getName()返回"Centaur"(例如),則C具有Unicode值67,和value最終被73

+0

我的錯誤,它實際上告訴我們取模量,但謝謝! – yarcenahs

0

我們需要看到您正在調用該類的代碼,並且正在生成您的錯誤消息。爲什麼期待0? 8似乎是您提供的信息的有效返回值。

相關問題