我目前正在为我的计算机科学课进行练习,但是在运行代码时仍然遇到这个顽固的问题。
我想出了一种数学上可以找到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());
}
}
这是Currency类中的代码
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之类的值(即不是2digits。2 2digits)
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);
}
}
}
关于java - Java包装器方法练习的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61464618/