获取字符串中第一个字符的unicode值

时间:2016-12-05 17:24:04

标签: java methods unicode logic getter

我基本上被要求获取字符串的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;
}

2 个答案:

答案 0 :(得分:0)

您已经说过,您应该将该值增加10%。但是,你实际上做的是通过仅占用10%来减少 90%(然后将其截断为67.0 * 0.1 = 6.7)。 int,截断为6时为0.1

1.1更改为int value = (int)((int)(getName().charAt(0)) * 1.1); // --------------------------------------------^ 增加 10%:

getName()

如果"Centaur"返回C(例如),67的Unicode值为value73最终为{{1} }}

答案 1 :(得分:0)

我们需要查看您正在调用该类的代码并生成错误消息。为什么期待0? 8似乎是您提供的信息的有效返回值。