By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,352 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,352 IT Pros & Developers. It's quick & easy.

Stuck on While Loop

P: 6
I am working on a class assignment called Pennies for Pay
the object of the program is to receive input for number of days worked.
This should be > 0 and <= 40.

An example output is below and you will notice, the pay doubles each day.

Pay: $.01 on day 1. Total Salary $.01
Pay: $.02 on day 2. Total Salary $.03
Pay: $.04 on day 3. Total Salary $.07
Pay: $.08 on day 4. Total Salary $.15
Pay: $.16 on day 5. Total Salary $.31
continue until number of days.

My program is below: The only problem I have is that when a user inputs the number of days worked, it goes from that day, all the way until 40 instead of from the first day until the number of days worked.

I think I may need to tweak the while loop....any suggestions?

Expand|Select|Wrap|Line Numbers
  1. import java.util.*; // This is the java util package that includes the Scanner class and many others to be implemented in Java.
  2.  
  3. import java.text.*; // This is the java text package that allow us to use the decimal format.
  4.  
  5. public class DoublePenny{
  6.  
  7. public static void main( String args[] ){
  8.  
  9. // Below we have four integers, two of which are double because we are using decimals.
  10.  
  11. // The amounts assigned to each are to provide a starting and ending point to reference later in the program.
  12.  
  13. int Days;
  14. int MaxDays = 40;
  15. double TotalSalary = .01;
  16. double Pay = .01;
  17.  
  18.  
  19.   // These three lines below this will display a text and allow the user to input a day in which to see the doubling of pay.
  20.  
  21. System.out.println("Please enter the number of days worked between 1 and 40 (NO OVERTIME!=) :");
  22.  
  23.  
  24.      Scanner keyboard = new Scanner( System.in ); //Allows input from user via the keyboard
  25.  
  26.     Days = keyboard.nextInt(); // This is to let the program know that the users number that they input will be the day amount.
  27.  
  28.  
  29.   // This while loop was constructed to note that the Days amount is less than the MaxDays and if any number greater than fourty is input,
  30. // the program will exit.
  31.  
  32.    while (Days <= 40){
  33.  
  34.     // This displays the amount of TotalSalary in two digit format, it cleans the numbers up a bit.
  35.  
  36.   DecimalFormat decimalTenth = new DecimalFormat("0.00");
  37.  
  38.      // The below display all of the amount that were previously assigned to the variables and labels them accordingly.
  39.  
  40.   System.out.println("\n Days Worked: " + Days + "     Pay : " + Pay + "       Total Salary: " + (decimalTenth.format(TotalSalary)));
  41.  
  42.  
  43.    Days++;
  44.    Pay*=2;
  45.    TotalSalary+=Pay;
  46.  
  47. }
  48. }
Mar 18 '08 #1
Share this Question
Share on Google+
12 Replies


Expert 100+
P: 849
Yep, you do need to tweak your while loop. As it is, it runs up to 40 days regardless of user input. You only want it to run up to the user's input, to a maximum of 40. Just have it run to user's input, but make the user re-enter if the input is over 40.

Also, please use the provided CODE tags when posting code to make it easier to read.
Mar 18 '08 #2

P: 9
Well, Gentleman , What exactly do you wanna do...
I mean, Do you wanna display the salary for the number of days one Worked or
The salary one would be earning next days until one finishes 40 days, including the number of days entered??
Mar 18 '08 #3

P: 6
Yep, you do need to tweak your while loop. As it is, it runs up to 40 days regardless of user input. You only want it to run up to the user's input, to a maximum of 40. Just have it run to user's input, but make the user re-enter if the input is over 40.

Also, please use the provided CODE tags when posting code to make it easier to read.
Could you explain the parameters of the while loop, Im not understanding how you run up to the users input to a maximum of 40?
Mar 18 '08 #4

Expert 100+
P: 849
Your code as it is runs from the user's input to 40. You need to initialize another variable, maybe daysWorked, to zero and use that as your counter. The maximum amount in the while loop should be Days, not 40, since the current setup checks for (40-input) days. What you need to do to get the input in the proper range is use another while loop. Are you familiar with the logical operators && and ||?
Mar 18 '08 #5

satch
P: 23
Could you explain the parameters of the while loop, Im not understanding how you run up to the users input to a maximum of 40?
In your assignment, going from 1 to user input is the same as going from user input to 1. Basically you have to double the pay for the number of days user has input. So it does not matter if you go from 1 to user input or from user input to 1, the pay gets doubled the same number of times.

So this is what you can do:
1. After reading the users input, check if it is in the valid range of (0,40]. If not then make the user re-enter or flag error whichever you may want to do.

2. If it is in the valid range then we go ahead to the while loop. And in the while loop decrement the 'Days' variable to go upto zero. So the while loop condition will now be 'while(Days > 0)'.

3. With this approach another small change that you'll have to make is in the output that you are printing. Store the user input in a variable say userInput. For the 'Days Worked' you'll have to print 'userInput-Days+1' instead of Days, which you are currently printing, if you want the Days Worked to go from 1 to user input in the output that your program prints.
Mar 19 '08 #6

Expert 10K+
P: 11,448
Note that on day 'i' the total amount of money paid is2^i-1 cents, where '^' is the
'to the power of' operator.

kind regards,

Jos
Mar 19 '08 #7

P: 6
Your code as it is runs from the user's input to 40. You need to initialize another variable, maybe daysWorked, to zero and use that as your counter. The maximum amount in the while loop should be Days, not 40, since the current setup checks for (40-input) days. What you need to do to get the input in the proper range is use another while loop. Are you familiar with the logical operators && and ||?
Yes sir, I tried using this as the while loop and it compiles but it wont execute

while( loopCounter <= days && loopCounter <= maxDays) { }

I cant figure out what I am missing here.
Mar 19 '08 #8

Expert 100+
P: 849
What do you mean it won't execute? Do you not get any output? Is the output that you get bad? Do you get a runtime error of some kind?
Mar 19 '08 #9

P: 6
What do you mean it won't execute? Do you not get any output? Is the output that you get bad? Do you get a runtime error of some kind?

No error, no bad output, after the user enters a number it just sits there.
Mar 19 '08 #10

P: 6
Would a "for" loop be better in this situation?
Mar 19 '08 #11

P: 6
and maybe sprinkle some if else statements in there to show an error message if user enters a wrong amount of days?
Mar 19 '08 #12

Expert 100+
P: 849
The if/else statements are a good idea. Can we see your updated code?
Mar 19 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.