import java.text.DecimalFormat;
import java.util.Scanner;
public class Abramyan_week5b{
public static void main( String[] args){
double vehiclePrice, vehicleMileage, vehicleAge, vehicleCond, deprPerMile, mileDepr, condPct, condDepr, appraisedPrice;
Scanner k = new Scanner(System.in);
DecimalFormat df1 = new DecimalFormat("#,##0.00");
System.out.print("Enter the purchasing price of the vehicle = ");
vehiclePrice = k.nextDouble();
System.out.print("Enter the current mileage of the vehicle = ");
vehicleMileage = k.nextDouble();
System.out.print("Enter the age of the vehicle in years = ");
vehicleAge = k.nextDouble();
System.out.print("Enter the vehicle condition ('1' for clean, '2' for average and '3' for rough) = ");
vehicleCond = k.nextDouble();
while ((vehicleCond != 1) || (vehicleCond != 2) || (vehicleCond != 3)){
System.out.print("Enter the vehicle condition ('1' for clean, '2' for average and '3' for rough) = ");
vehicleCond = k.nextDouble();
if (vehicleCond == 1){
condPct = 0.02;
break;
}
else if (vehicleCond == 2){
condPct = 0.03;
break;
}
else if (vehicleCond == 3){
condPct = 0.04;
break;
}
}
if (vehiclePrice > 60000)
deprPerMile = 0.6;
else if (vehiclePrice > 40000)
deprPerMile = 0.5;
else if (vehiclePrice > 30000)
deprPerMile = 0.4;
else if (vehiclePrice > 20000)
deprPerMile = 0.3;
else
deprPerMile = 0.2;
if (vehicleCond == 1)
condPct = 0.02;
else if (vehicleCond == 2)
condPct = 0.03;
else
condPct = 0.04;
mileDepr = vehicleMileage * deprPerMile;
condDepr = vehiclePrice * vehicleAge * condPct;
appraisedPrice = vehiclePrice - mileDepr - condDepr;
System.out.println(" The depreciated value of the vehicle for mileage is $" + df1.format(mileDepr));
System.out.println(" The depreciated value of the vehicle for the condition is $" + df1.format(condDepr));
System.out.println(" The current appraised value of the vehicle is $" + df1.format(appraisedPrice));
}
}
您的答案几乎是正确的,要使它起作用,您只需要更改条件,但让我们看看如何改进它。
您可以看到,我将代码从main中取出,并将其放在一种称为
-startprogram的方法中。在这种情况下,效果并不那么明显,因为没有属性或其他方法,如果有任何属性或其他方法,则必须将其声明为static
,如果代码在main。 将几个带有数组的几个Double变量重新定位,并且通过这种方式,我也使用forfor.来限制重复代码。
在中仅让wevehiclecond实例化。 是相等的(替换对vehiclePrice,vehiclemileage的参考和vehicleage
vEhicleagevervalues[0],vehiclevalues [1]和vehiclevalues [1]和pehicleValues [2][2]
)。