First question:
I wrote a program to calculate gift tax. When tested with input value: 14491 the output was -25436.3. I can't figure out how this number was calculated. Can anyone help explain this? If I'm not mistaken the code should have calculated parentheses then multiplied and then added. I'm also a noob, so I'm probably wrong but would love feedback.
Second question:
Is there a better way to have written this code?
import java.util.Scanner;
public class GiftTax {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Value of the gift?");
double amt = scan.nextDouble();
if (amt < 5000) {
System.out.println("No tax!");
} else if (amt == 5000 && amt <= 25000) {
System.out.println("Tax: " + (100 + (amt - 5000) * .08));
} else if (amt == 25000 && amt <= 55000) {
System.out.println("Tax: " + (1700 + (amt - 25000) * .10));
} else if (amt == 55000 && amt <= 200000) {
System.out.println("Tax: " + (4700 + (amt - 55000) * .12));
} else if (amt == 200000 && amt <= 1000000) {
System.out.println("Tax: " + (22100 + (amt - 200000)* .15));
} else {
System.out.println("Tax: " + (142100 + (amt - 1000000) * .17));
}
}
}
I changed "==" in the code to ">=" and it worked perfectly. I just don't understand how it came up with that initial negative number and it's driving me mad...
Thanks everyone!
Since 14491 is not less than 5000, it runs through all the else/if checks but 14491 is not EXACTLY equal to 5000, 25000, 55000, or 200000, so only the LAST ELSE block is what gets executed:
The calculation is: