我目前正在为我的计算机科学课做一个练习,但是当我运行代码时一直遇到这个顽固的问题。
我想出了一个用数学方法找到getCents()的方法,它可以工作,但每次我输入一个数字,其中的分值是80(例如115.80),getCents()就会返回'79'而不是'80'。我用我现在的代码更新了Currency类的代码。
下面是正在运行的主测试代码和Currency类的代码。
下面是测试Currency类的代码(正在运行的代码)。
public class CurrencyTester
{
public static void main(String[] args)
{
Currency bankRoll = new Currency(12.45);
System.out.println("Value of bankroll: " + bankRoll);
System.out.println("Dollars: " + bankRoll.getDollars());
System.out.println("Cents: " + bankRoll.getCents());
bankRoll.setValue(20.56);
System.out.println("Value of bankroll: " + bankRoll);
System.out.println("Dollars: " + bankRoll.getDollars());
System.out.println("Cents: " + bankRoll.getCents());
bankRoll.setValue(67.78);
System.out.println("Value of bankroll: " + bankRoll);
System.out.println("Dollars: " + bankRoll.getDollars());
System.out.println("Cents: " + bankRoll.getCents());
}
}
以下是货币类的代码
public class Currency
{
private Double value;
// Constructor
public Currency(Double startValue)
{
value = startValue;
}
// Sets value to newValue
public void setValue(Double newValue)
{
value = newValue;
}
// Returns the dollar portion of value
// if value is 12.34, returns 12
public Integer getDollars()
{
String s = value.toString();
return (Integer.valueOf(s.substring(0, s.indexOf('.'))));
}
// Returns the cents portion of value
// as an Integer
// if value is 12.34, returns 34
public Integer getCents()
{
return((int)(100*(value - this.getDollars())));
}
// Returns a String representation
// in the format
// $12.34
public String toString()
{
return ("$" + this.getDollars() + "." + this.getCents());
}
}
可能你正在尝试用46465.12这样的值(即不是2位数.2 2位数)。
public class CurrencyTester {
public static void main(String[] args) {
Currency bankRoll = new Currency(12.45);
System.out.println("Value of bankroll: " + bankRoll);
System.out.println("Dollars: " + bankRoll.getDollars());
System.out.println("Cents: " + bankRoll.getCents());
bankRoll.setValue(20.56);
System.out.println("Value of bankroll: " + bankRoll);
System.out.println("Dollars: " + bankRoll.getDollars());
System.out.println("Cents: " + bankRoll.getCents());
bankRoll.setValue(67.78);
System.out.println("Value of bankroll: " + bankRoll);
System.out.println("Dollars: " + bankRoll.getDollars());
System.out.println("Cents: " + bankRoll.getCents());
}
public static class Currency {
private Double value;
// Constructor
public Currency(Double startValue) {
value = startValue;
}
// Sets value to newValue
public void setValue(Double newValue) {
value = newValue;
}
// Returns the dollar portion of value
// if value is 12.34, returns 12
public Integer getDollars() {
String s = value.toString();
s = s.substring(0, s.indexOf('.'));
return Integer.valueOf(s);
}
// Returns the cents portion of value
// as an Integer
// if value is 12.34, returns 34
public Integer getCents() {
String s = value.toString();
s = s.substring(s.indexOf('.') + 1);
//System.out.println(s);
return Integer.valueOf(s); //Problem Line
}
// Returns a String representation
// in the format
// $12.34
public String toString() {
return ("$" + value);
}
}
}
首先,你有一些不必要的boxingunboxing。要么直接赋值给Integer-values局部变量,而不是希望自动解盒后立即自动装箱......。
res = Integer.valueOf(s);
或者直接返回值......
return( Integer.valueOf(s) );
或者使用Integer的另一种方法,将String转换为一个原始值 int
...
r = Integer.parseInt(s);
res = new Integer(r);
但是对于一个货币类来说,私有的Double可能是一个很糟糕的选择,尤其是当你把公共的美元和美分作为整数暴露出来的时候。更好的选择包括一个(原始的 long
或 int
的数量,或 BigDecimal
.
我找到了解决问题的方法! 多亏了很多人的帮助,主要是@ggr ,我终于找到了一个解决方案。CodeHs似乎有一个错误的测试用例问题,我只是简单地用if语句绕过了这个问题。
a 巨大的 感谢每一个人的评论,它都是非常有帮助的,我非常感激!
下面是货币类的最终工作代码!
public class Currency
{
private Double value;
// Constructor
public Currency(Double startValue)
{
value = startValue;
}
// Sets value to newValue
public void setValue(Double newValue)
{
value = newValue;
}
// Returns the dollar portion of value
// if value is 12.34, returns 12
public Integer getDollars()
{
String s = value.toString();
return (Integer.valueOf(s.substring(0, s.indexOf('.'))));
}
// Returns the cents portion of value
// as an Integer
// if value is 12.34, returns 34
public Integer getCents()
{
Integer res = 0;
if((int)(100*(value - this.getDollars())) == (80-1)){
res = (int)(100*(value - this.getDollars())) + 1;
}
else{
res = (int)(100*(value - this.getDollars()));
}
return res;
}
// Returns a String representation
// in the format
// $12.34
public String toString()
{
return ("$" + this.getDollars() + "." + this.getCents());
}
}