Re: Java Looping Question
I think I can use a for statement according to my instructor, and I based this next line of code from an example of his, but I keep getting an error:
Code:
for (double counter = annualSales; counter < counter * 1.5; counter +5000)
{
System.out.println("Stuff...");
}
The error says not a statement, but I'm not sure what I am doing wrong. annualSales is a variable I already declared earlier.
Re: Java Looping Question
Code:
for (double counter = annualSales; counter < counter * 1.5; counter +5000)
{
System.out.println("Stuff...");
}
That should be an assignment at the end of the if:
Code:
counter = counter + 5000
But you'll also hit a problem with the check in the middle:
Code:
counter < counter * 1.5
counter is never going to be anything other than less than itself times 1.5.
I think you want to check it against annualSales * 1.5
Re: Java Looping Question
Awesome, thank you so much for that. I have one more question about the output code:
Code:
for(double counter = annualSales; counter < annualSales * 1.5;
counter +=5000)
{
System.out.println("Total Sales: " + counter + "\tTotal "
+ "Compensation: " + someTypeOfCalculation(counter);
}
Where I put "someTypeOfCalculation", I'm not exactly sure what to put there. You see, if the annual sales that one enters is < 96,000, they earn no commission...if the annual sales is between 96,000 and 120,000, they earn a 25% commission, and if the annual sales is above 120,000, they earn 31.25% commission.
I made an if-else-if statement before the loop code that performed these calculations separately, and now I really don't know what to do so it calculates and displays all of these correctly.
I can post the entire code if it would help.
Re: Java Looping Question
Well, the way you have it written there is actually quite good.
Now, just write a method with the same name (OK, you might want to use a different name than someTypeOfCalculation).
That method will return a value (String, double, up to you).
It will do exactly what you did in the previous if/else, but return a suitable value for each of these cases.
Re: Java Looping Question
Hmm, the "someTypeOfCalculation(counter)" part really confuses me. All of my calculations were correct in the if-else-if statement, but I'm not sure how to write this because the commission rate has the potential to change due to the "50% above the salesperson's annual sales" part. For example: if the salesperson enters $120,000, then he earns a 25% commission, but when it goes up $5,000 (to $125,000 total), he/she would earn a 31.25% commission rate.
Will I need to write multiple System.out.println() statements or can I fit this into one calculation? I wonder if I am overthinking this...sorry, it's only my 3rd week of learning Java and my brain goes haywire at times trying to figure it all out, lol.
Re: Java Looping Question
Quote:
Originally Posted by
Tolls
But you'll also hit a problem with the check in the middle:
Code:
counter < counter * 1.5
counter is never going to be anything other than less than itself times 1.5.
Variable 'counter' could (theoretically) be negative ...
kind regards,
Jos
Re: Java Looping Question
Quote:
Originally Posted by
rhym1n
Hmm, the "someTypeOfCalculation(counter)" part really confuses me. All of my calculations were correct in the if-else-if statement, but I'm not sure how to write this because the commission rate has the potential to change due to the "50% above the salesperson's annual sales" part. For example: if the salesperson enters $120,000, then he earns a 25% commission, but when it goes up $5,000 (to $125,000 total), he/she would earn a 31.25% commission rate.
Will I need to write multiple System.out.println() statements or can I fit this into one calculation? I wonder if I am overthinking this...sorry, it's only my 3rd week of learning Java and my brain goes haywire at times trying to figure it all out, lol.
Your method would be something like:
Code:
if sales < somevalue then
return 10% of sales
else if sales < thenextvalue then
return 20% of sales
else
return 25% of sales
Re: Java Looping Question
You can put if-else-if statements within a loop?
This is what I am having trouble with, the someTypeOfCalculation part (which isn't really a variable):
Code:
for(double counter = annualSales; counter < annualSales * 1.5;
counter +=5000)
{
System.out.println("Total Sales: " + counter + "\tTotal "
+ "Compensation: " + someTypeOfCalculation;
}
Re: Java Looping Question
Does anyone know about the last part?
Re: Java Looping Question
You had it before, with someTypeOfCalculation(counter).
Have you done methods?