440,352 Members | 1,593 Online
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 import java.util.*; // This is the java util package that includes the Scanner class and many others to be implemented in Java.   import java.text.*; // This is the java text package that allow us to use the decimal format.   public class DoublePenny{   public static void main( String args[] ){   // Below we have four integers, two of which are double because we are using decimals.   // The amounts assigned to each are to provide a starting and ending point to reference later in the program.   int Days; int MaxDays = 40; double TotalSalary = .01; double Pay = .01;       // 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.   System.out.println("Please enter the number of days worked between 1 and 40 (NO OVERTIME!=) :");          Scanner keyboard = new Scanner( System.in ); //Allows input from user via the keyboard       Days = keyboard.nextInt(); // This is to let the program know that the users number that they input will be the day amount.       // 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, // the program will exit.      while (Days <= 40){       // This displays the amount of TotalSalary in two digit format, it cleans the numbers up a bit.     DecimalFormat decimalTenth = new DecimalFormat("0.00");        // The below display all of the amount that were previously assigned to the variables and labels them accordingly.     System.out.println("\n Days Worked: " + Days + "     Pay : " + Pay + "       Total Salary: " + (decimalTenth.format(TotalSalary)));        Days++;    Pay*=2;    TotalSalary+=Pay;   } }  } Mar 18 '08 #1
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

 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