使用循环计算双倍递减折旧
Calculating double diminishing depreciation using loops
所以我必须想出一个使用双递减法计算折旧的代码。
到目前为止我得到了这个代码:
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);
}
所以我必须想出一个使用双递减法计算折旧的代码。
到目前为止我得到了这个代码:
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);
}