【发布时间】:2023-03-09 01:55:01
【问题描述】:
所以我必须想出一个使用双递减法计算折旧的代码。
到目前为止,我得到了这个代码:
Scanner kbd = new Scanner (System.in);
System.out.println("Please enter asset number: ");
assetNum = kbd.nextInt();
System.out.println("Please enter initial purchase price: ");
purchPrice = kbd.nextDouble();
System.out.println("Please enter useful life of the asset (in years): ");
usefulLife = kbd.nextDouble();
System.out.println("Please enter the salvage value: ");
salvageVal = kbd.nextDouble();
System.out.println("Please enter the number of years of depreciation: ");
numYears = kbd.nextDouble();
ddRate = ((1.0 / usefulLife) * 2) * 100;
System.out.println("Asset No: " + assetNum);
System.out.printf("Initial Purchase Price: $%,.0f%n" , purchPrice);
System.out.printf("Useful Life: %.0f years%n" , usefulLife);
System.out.printf("Salvage Value: $%,.0f%n" , salvageVal);
System.out.printf("Double Declining Rate: %.0f%%%n" , ddRate);
System.out.printf("Number of Years: %.0f years%n" , numYears);
System.out.println();
System.out.println("Year Yearly Accumulated Book");
System.out.println(" Depreciation Depreciation Value");
System.out.println();
int year;
double yearlyDepr;
double accDepr;
double bookVal;
bookVal = purchPrice;
accDepr = 0;
year = 0;
while (bookVal >= salvageVal){
yearlyDepr = bookVal * (ddRate / 100);
accDepr = accDepr + yearlyDepr;
bookVal = bookVal - yearlyDepr;
year++;
System.out.printf("%d %,18.0f %,18.0f %,18.0f%n" , year, yearlyDepr, accDepr, bookVal);
}
}
}
输出看起来不错,直到最后一行的账面价值为 7,776,而不是残值 10,000。
这样的地雷: http://puu.sh/zyGzg/e35ccf0722.png
应该是这样的: http://puu.sh/zyGBM/4b6b8fa14c.png
请帮忙,我真的卡住了。
【问题讨论】:
-
在您的 while 循环中,您需要测试新的
bookVal是否小于salvageVal,如果是,请使用salvageVal并打破循环 -
我试图理解“应该是这样的”图形。是否存在一旦资产达到残值就停止折旧的业务规则?如果是这种情况,您需要添加一些逻辑来检查此条件。
-
是的,停止条件是当账面价值等于残值时
标签: java loops while-loop accounting