How to take discount calculation

  • A+

Very new to programming. I would want the user to receive a 10% discount if he/she is over 60 (age>60) and 5% discount if he/she is between over 55 and equal to 60. (60<=age>55). I know that my code is completely wrong, but I would like to fix this step by step if possible.

import java.util.*; public static void main(String[] args) {  Scanner input = new Scanner(; int price, age; double tax, payAmount; double discountRate_56_to_60 = 0.05; double discountRate_60_above = 0.1; payAmount = price * tax;  System.out.print("Price?"); price = input.nextInt();  System.out.print("Tax(%)?"); tax = input.nextDouble();  System.out.print("Age?"); age = input.nextInt();  System.out.println("You pay: $"); payAmount = input.nextDouble();  if (age > 55) {     payAmount; } else if (age >= 60) {     payAmount; }  } } 


There are multiple problems, the first one is that the second if statement else if (age >= 60) is never reached, because if the age is over 60 the person is also over 55. To solve that you should change the statements like this:

if(age >+ 60){    // do something } else if(age >= 55){    // do something different } 

Then you try to initialize payAmount before you have initialized price and tax. You should to the calculation after the User entered his age and the price etc.

// User enters all the stuff payAmount = price * tax; 

And then you can apply the right discount rate in the if statements.

And if I would be you I would declare the Integer price as a Double, because it could be that the price is like 16.50$.

Also you could check if the entered number is an integer or a double to avoid exceptions.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: