使用年数总和的Java折旧表循环

时间:2017-03-26 20:34:51

标签: java

将三个参数传递到此函数以输出折旧表。第一个循环计算年份的总和,而第二个循环将计算用户输入的项目的使用寿命期间每年的折旧。我在编写第二个循环时遇到问题,因此它会在上面的System.out.println语句的标题下输出正确的值。因为循环不会运行,因为它与第一个循环相同。

public class Table
    {
    public void makeDepreciationTable( int useful_Life, double acquisition_Value, double salvage_Value )
    {
        int sum = 0;
        int year = 1;
        double accumulatedDepreciation;
        //  make a loop to calculate the sum
        while ( year <= useful_Life )
        {
            sum += year;
            year++;
        }
        System.out.println("The sum is " +sum );
        // write the header of the table
        System.out.println(" Acquisition Value" + "" + " Salvage Value" + "" + " Useful Life" + "" + " Annual Depreciation" + "" + " Accumulated Depreciation" + "" + " Book Value" + "" +" Fraction");
        // make a loop
        while ( year <= useful_Life )
        {
            // calculate fraction
            double fraction = (double)year/sum;
            // calculate annualDepreciation
            double annualDepreciation = (acquisition_Value - salvage_Value) * fraction ;
            // calc accumulatedDepreciation
            accumulatedDepreciation += annualDepreciation;
            // calc bookValue
            double bookValue = acquisition_Value  - accumulatedDepreciation;
            // write one line of table
            System.out.println(acquisition_Value + "" + salvage_Value + "" + useful_Life + "" + annualDepreciation + "" + accumulatedDepreciation + "" + bookValue + "" + fraction);
            year++;
        }



    }
}

2 个答案:

答案 0 :(得分:0)

因为year在第一个while循环结束时变得等于useful_Life,所以需要在第二个{{}之前再次将year值重置为1 {1}}循环,如下所示:

while

答案 1 :(得分:0)

在第二次循环之前添加年份= 1。

由于第一个循环年份值增加。但是在第二次循环之前没有将它设置回一个