第五章第四十二题(金融应用:求销售额)(Financial application: find the sales amount)
*5.42(金融应用:求销售额)如下重写编程练习题5.39: 使用for循环替代do-while循环。 允许用户自己输入COMMISSION_SOUGHT而不是将它固定为一个常量。 *5.42 (Financial application: find the sales amount) Rewrite Programming Exercise 5.39 as follows: Use a for loop instead of a do-while loop. Let the user enter COMMISSION_SOUGHT instead of fixing it as a constant.
参考代码:
package chapter05;import java.util.Scanner;publicclassCode_42{publicstaticvoidmain(String[] args){finaldouble COMMISSION_SOUGHT;int salesAmount =10000;double mySalary =0;
System.out.print("Enter your base salary: ");
Scanner inputScanner =newScanner(System.in);
COMMISSION_SOUGHT = inputScanner.nextDouble();for(;mySalary <30000;salesAmount++)
mySalary = COMMISSION_SOUGHT +5000*0.08+5000*0.10+(salesAmount-10000)*0.12;
System.out.printf("The minimum sales you have to generate is %d", salesAmount);}}
结果显示:
Enter your base salary:5000
The minimum sales you have to generate is 210835
Process finished with exit code 0