我刚接触java编程我需要帮助打印出折扣后的汽车价格。目前显示折扣金额。
public class carApplication {
public static void main(String[] args) {
Car car1 = new Car("Toyota", "2012", 13500.00);
Car car2 = new Car("Audi", "2011", 12000.00);
System.out.printf("Car 1 Model: %s; Car year: %s; Car Price: %.2f\n",
car1.getModel(), car1.getYear(), car1.getcarPrice());
System.out.printf("Car 1 Model: %s; Car year: %s; Car Price: %.2f\n",
car2.getModel(), car2.getYear(), car2.getcarPrice());
System.out.println("Car Price after applying discount");
car1.setcarPrice(car1.getcarPrice()*(0.05));
car2.setcarPrice(car2.getcarPrice()*(0.07));
System.out.printf("Car 1 Model: %s; Car year: %s; Car Price: %.2f\n",
car1.getModel(), car1.getYear(), car1.getcarPrice());
System.out.printf("Car 1 Model: %s; Car year: %s; Car Price: %.2f\n",
car2.getModel(), car2.getYear(), car2.getcarPrice());
}
}
答案 0 :(得分:0)
@Mkreegs是对的,你应该写
car1.setcarPrice(car1.getcarPrice()*(1 - 0.05));
car2.setcarPrice(car2.getcarPrice()*(1 - 0.07));
或通俗易懂:
car1.setcarPrice(car1.getcarPrice()*(0.95));
car2.setcarPrice(car2.getcarPrice()*(0.93));