Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用年和的Java折旧表循环_Java - Fatal编程技术网

使用年和的Java折旧表循环

使用年和的Java折旧表循环,java,Java,将三个参数传递到此函数以输出折旧表。第一个循环计算年份之和,而第二个循环将计算用户输入的项目使用寿命期间每年的折旧。我在编码第二个循环时遇到了问题,因此它无法在上述System.out.println语句的标题下输出正确的值。按原样,循环不会运行,因为它与第一个循环相同 public class Table { public void makeDepreciationTable( int useful_Life, double acquisition_Value, double

将三个参数传递到此函数以输出折旧表。第一个循环计算年份之和,而第二个循环将计算用户输入的项目使用寿命期间每年的折旧。我在编码第二个循环时遇到了问题,因此它无法在上述
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++;
        }



    }
}
公共类表
{
公共void makedevalentionTable(int有用年限、双倍收购价值、双倍残值)
{
整数和=0;
整年=1;
双重增值;
//做一个循环来计算总和

而(年因为
在第一个while循环结束时等于
有效寿命
您需要在第二个
while
循环结束前再次将
值重置为1,如下所示:

year = 1;//reset year before 2nd while
while ( year <= useful_Life ) {
   //add your code
}
year=1;//重置第二年之前的年份

而(年份在第二个循环之前加上年份=1


因为第一个循环的年份值是递增的。但在第二个循环之前没有将其设置回1

欢迎使用堆栈溢出!问题寻求调试帮助(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现该问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。不清楚“总和”是什么假设表示。例如,如果有用寿命为3,则第一个循环将导致总和为1+2+3=6。为什么要使用循环进行此计算?一个简单的公式就足够了。