Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Java Tax calculator help

Can anyone help me with this code? I think it needs simplifying and re working. I can tell it doesn’t work due to £160 tax comes out as £6 when it’s meant to be £3.50 rounded to £4.

import java.util.Scanner;
public class Taxes {

public static void main(String[] args) {
System.out.println("What is your income?");
int income = 100;
Scanner sc = new Scanner(System.in);
income = sc.nextInt();
if (income < 100) {

System.out.println(income%100*0);

} else if (income >= 101 && income <= 150) {

System.out.println(income%100*0.05);

} else if(income >= 151 && income <= 200) {

System.out.println(income%100*0.1);

} else if(income >= 201 && income <= 300) {

System.out.println(income%100*0.15);

} else if(income >= 301 && income <= 401) {

System.out.println(income%100*0.2);

} else {

System.out.println(income%100*150);
}
}
}
[Image: tumblr_n4n755C9Ms1sh78fuo1_400.gif]
looks like your math formulas could be at fault, try changing them around.
Code
else if(income >= 151 && income <= 200) {

System.out.println(income%100*0.1);

}

The result of this operation 160%100*0.1 is indeed 6, as poster above said you have to change the math
Surly it is because you have set income to be of type int. You probably want it as a float or double instead.
If you were looking to tax a percentage,  like if you wanted to tax 10% off 160 pound, you can just print 160*.01 which is 10% of 160.

If you do 160%100, you're returning the remainder from taking out 100 out of 160 until it can no longer take out 100 anymore.

160%100 = 60.

160*.01 = 16

160%100*0.1 => 60*.01 which is 10% of 60, resulting in 6.

Change the math.

it seems that you just didn't know how modulus works.

Say 10%2, its equal to 0, because you can keep taking 2 out of 10 until it reaches 0.

while 9%2 is equal to 1, because if you keep removing 2 out of 9 you'll eventually reach 1, which is lower than 2, meaning you can't take 2 from 1, meaning 1 is the remainder.

while 160%100, you're taking out 100 until you can't take it out anymore and return the remainder.

160 - 100 = 60, you can't take out 100 out of 60, so 60 is your remainder, hence 160%100 = 60.

260%100 is also 60, because 260-100 = 160, then take out 100 again ,160-100 = 60.
[Image: 5WnumJy.png]

bitcoin:14L3Af8XPZRbmbcfEZTFpCZbHv2D7ujdAy
This is a big mess. Aside from your misuse of the modulus operator, your application of tax bands/brackets is fundamentally wrong. This is why you will not get the numbers you expect, even after fixing modulus.

Tax brackets are applied in stages, not all at once. If you earn £160, and the tax rate from £150-£200 is 10%, you are not taxed £160*0.10=£16. You are taxed 0% on the first £100, 5% on the next £50, and 10% on the remaining £10. That's £0.00+£2.50+£1.00=£3.50.

Taxes wouldn't make sense if they were calculated the way you're doing it.

:)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by AccoyOct 30, 2015 11:38 AM 2 151 Oct 30, 2015 11:38 AM
Last Post: StayTech666
[Java] by xlosmuertosxSep 25, 2015 09:02 PM 7 241 Sep 25, 2015 09:02 PM
Last Post: xlosmuertosx
[Java] by CyberPunkwMar 16, 2015 01:30 AM 18 465 Mar 16, 2015 01:30 AM
Last Post: Psifour