java - Java包装器方法练习的问题

标签 java compiler-errors wrapper

我目前正在为我的计算机科学课进行练习,但是在运行代码时仍然遇到这个顽固的问题。

我想出了一种数学上可以找到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/

相关文章:

java - 查明插件内部是否使用了 android java 类

angular - 向angular2迁移到最终版本时出现未处理的promise拒绝错误

python - 使用 ctypes 将 python IplImage 对象作为简单结构传递给共享 C 库

c++ - 使用自定义 std::ostream 包装 FILE*

java - 队列上的监听器拾取消息时的时间戳

java - GWT RequestFactory - 向代理类添加自定义方法?

excel - 范围查找替换VBScript excel 编译错误缺少说明

python - 包装对象的最佳 Python 方式是什么?

java - AutoCompleteSupportFragment 在选择位置后不断禁用它的自身

c++ - std::unordered_map 插入错误