获取字符串中第一个字符的unicode值
Getting the unicode value of the first character in a string
我基本上被要求获取字符串的 Unicode 值,将其乘以 10%,然后加上对象当前的任何级别。令人沮丧的是,事实证明我已将逻辑包括代码在内,但我仍然收到一条错误消息:expected:<0> but was:<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;
}
您说过您应该增加 10% 的价值。不过,您实际上在做的是 减少 90%,只取其中的 10%(然后将其截断为 int
)。 67.0 * 0.1 = 6.7
,截断为 int
时为 6
。
将 0.1
更改为 1.1
以 增加 10%:
int value = (int)((int)(getName().charAt(0)) * 1.1);
// --------------------------------------------^
在那里,如果 getName()
returns "Centaur"
(例如),C
具有 Unicode 值 67
,并且 value
最终成为 73
.
我们需要查看您调用 class 时使用的代码,它会生成您的错误消息。为什么期望为 0?根据您提供的信息,8 似乎是一个有效的 return 值。
我基本上被要求获取字符串的 Unicode 值,将其乘以 10%,然后加上对象当前的任何级别。令人沮丧的是,事实证明我已将逻辑包括代码在内,但我仍然收到一条错误消息:expected:<0> but was:<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;
}
您说过您应该增加 10% 的价值。不过,您实际上在做的是 减少 90%,只取其中的 10%(然后将其截断为 int
)。 67.0 * 0.1 = 6.7
,截断为 int
时为 6
。
将 0.1
更改为 1.1
以 增加 10%:
int value = (int)((int)(getName().charAt(0)) * 1.1);
// --------------------------------------------^
在那里,如果 getName()
returns "Centaur"
(例如),C
具有 Unicode 值 67
,并且 value
最终成为 73
.
我们需要查看您调用 class 时使用的代码,它会生成您的错误消息。为什么期望为 0?根据您提供的信息,8 似乎是一个有效的 return 值。