NOTE: Totally should have posted this in the "WHAT'S WRONG WITH MY CODE" thread, huh? Derp. >.< Don't see any way to move it, but I will pay more attention to where I'm posting next time.
Hey y'all, thanks for reading. I need to get this darn homework done within the next, ummm, 20ish hours, and I've hit a real snag.
Okay, so. I have done the following things:
1. Read the bejesus out of the chapter we're working on right now for the class, then re-read the sections that apply to if and if-else statements.
2. Left the problem for an entire day and come back to it, because god knows I wasn't exactly "at my best" by the end of the first frustrating session of slogging away and getting error messages.
3. Tried to focus on each individual block of code and make sure each part is correct.
4. Gone back and done all that stuff again.
I'm still having a problem. As far as I can tell, the code isn't "wrong" (and it DOES compile, and it DOES execute)...but I'm not getting the proper result.
I had a couple other people check my math, and they say it's right (mind you, we're all liberal arts kids, so if someone here finds a problem in my math at this point...I wouldn't be entirely surprised).
Here's the problem assigned by my instructor:
Print a random income (different at each execution) between $0 and $200,000, how much income tax is owed on it, and what the effective tax rate is. The base rate of income tax is 26%, with two exceptions: the first $20,000 of income is tax-free, and incomes of $100,000 and over are subject to an additional $10,000 special assessment. Examples: On $120,000 income, tax is $36,000, for an effective tax rate of 30%. On $12,000 income, tax is $0, for an effective rate of 0%.
// A program to do a bunch of math stuff (ahh lord make it stop). public class Homework3 { public static void main(String[] args) { // Randomly generate an "income" between $0 and $200,000. NOTE: Not entirely sure I did this part correctly, though I'm feeling hopeful. int income = (int)(Math.random() * ((200000 - 0) + 1); /* If income is $20,000 or less, no tax is applied. Effective tax rate is 0%. */ if (income <= 20000) { int tax = 0; System.out.println("If your income is " + income + ", you owe $" + tax + " income tax for an effective tax " + "rate of 0%."); } /* If income is greater than $20,000 and less than $100,000, a 26% tax is applied. Effective tax rate is 26%. */ else if (income > 20000 && income < 100000) { int tax = (int)(income * .26); System.out.println("If your income is " + income + ", you owe $" + tax + " income tax for an effective tax " + "rate of 26%."); } /* If income is $100,000 or more, a 26% income tax is applied along with a $10,000 special assessment. Effective tax rate is the tax amount divided by the original income. NOTE: This is the math bit I'm not sure of; I need to take a basic math class again, I suck at it pretty horrifically. */ else if (income >= 100000) { int adjustedIncome = (income - 20000); int tax = (int)(adjustedIncome * .26 + 10000); int effectiveTaxRate = (tax / income); System.out.println("If your income is " + income + ", you owe " + tax + " in come taxes for an " + "effective tax rate of " + effectiveTaxRate + "%."); } /*If the program comes up with a number that doesn't fall into any of these ranges, you obviously broke it. Way to go. */ else { System.out.println("There has " + "been an error, probably " + "somehow your fault. Jerk."); } } }
My problem is that the program SEEMS to be working...but then when it kicks out a number in the third category (>= 100,000), it gives a tax amount that would be appropriate for an income <= 20,000, and says the effective tax rate is 0.
So it seems to me that for some reason my program is ONLY using the first block of code, regardless of how big the number is. But I can't figure out WHY. >.< The only reason I specifically know of for that happening is when you put a semicolon at the end of the if statement, but I haven't done that.
Can anyone point me toward where the problem might be? I'm sure it's simple and probably obvious to anyone who's been doing this longer, but I still can't see it.
Thanks again for reading,
crys