Stuck on While Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beatjunkie27
    New Member
    • Mar 2008
    • 6

    Stuck on While Loop

    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?

    [CODE=java]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.prin tln("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.nextIn t(); // 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.prin tln("\n Days Worked: " + Days + " Pay : " + Pay + " Total Salary: " + (decimalTenth.f ormat(TotalSala ry)));


    Days++;
    Pay*=2;
    TotalSalary+=Pa y;

    }
    }
    }[/CODE]
    Last edited by Ganon11; Mar 18 '08, 08:57 PM. Reason: Please use the [CODE] tags provided.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    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.

    Comment

    • Asad
      New Member
      • Oct 2006
      • 9

      #3
      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??

      Comment

      • beatjunkie27
        New Member
        • Mar 2008
        • 6

        #4
        Originally posted by Laharl
        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?

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          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 ||?

          Comment

          • satch
            New Member
            • Feb 2008
            • 23

            #6
            Originally posted by beatjunkie27
            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.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              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

              Comment

              • beatjunkie27
                New Member
                • Mar 2008
                • 6

                #8
                Originally posted by Laharl
                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.

                Comment

                • Laharl
                  Recognized Expert Contributor
                  • Sep 2007
                  • 849

                  #9
                  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?

                  Comment

                  • beatjunkie27
                    New Member
                    • Mar 2008
                    • 6

                    #10
                    Originally posted by Laharl
                    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.

                    Comment

                    • beatjunkie27
                      New Member
                      • Mar 2008
                      • 6

                      #11
                      Would a "for" loop be better in this situation?

                      Comment

                      • beatjunkie27
                        New Member
                        • Mar 2008
                        • 6

                        #12
                        and maybe sprinkle some if else statements in there to show an error message if user enters a wrong amount of days?

                        Comment

                        • Laharl
                          Recognized Expert Contributor
                          • Sep 2007
                          • 849

                          #13
                          The if/else statements are a good idea. Can we see your updated code?

                          Comment

                          Working...