所以我必须提出一个使用双减法计算折旧的代码。
到目前为止我得到了这段代码:
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
while (bookVal > salvageVal){ // also change
yearlyDepr = bookVal * (ddRate / 100);
accDepr = accDepr + yearlyDepr;
bookVal = bookVal - yearlyDepr;
year++;
if (bookVal < salvageVal) {
bookVal = salvageVal;
yearlyDepr = purchPrice - salvageVal;
}
System.out.printf("%d %,18.0f %,18.0f %,18.0f%n" , year, yearlyDepr, accDepr, bookVal);
}