![]() |
|
[Java] Java Tax calculator help - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Java Tax calculator help (/showthread.php?tid=5903852) |
Java Tax calculator help - Mordicia - Nov 5, 2018 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); } } } RE: Java Tax calculator help - Mr. Miyagi - Nov 5, 2018 looks like your math formulas could be at fault, try changing them around. RE: Java Tax calculator help - TCP - Nov 5, 2018 Code else if(income >= 151 && income <= 200) {The result of this operation 160%100*0.1 is indeed 6, as poster above said you have to change the math RE: Java Tax calculator help - 333bitax0d9 - Nov 21, 2018 Surly it is because you have set income to be of type int. You probably want it as a float or double instead. RE: Java Tax calculator help - Varius - Nov 21, 2018 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. RE: Java Tax calculator help - Minesweeper - Nov 22, 2018 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. |