473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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?

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
12 3195
Laharl
849 Expert 512MB
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
Asad
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
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
Laharl
849 Expert 512MB
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
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
JosAH
11,448 Expert 8TB
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
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
Laharl
849 Expert 512MB
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
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
Would a "for" loop be better in this situation?
Mar 19 '08 #11
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
Laharl
849 Expert 512MB
The if/else statements are a good idea. Can we see your updated code?
Mar 19 '08 #13

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

Similar topics

1
by: ron | last post by:
have been stuck on this for several days now. I am trying to create a reverse polish calculator and I'm stuck at an intermediate stage. This is what I know I have to do (just not sure how to do it...
2
by: Oli | last post by:
Hi Here is the problem section of code..... <% else intOrderID = cstr(Session("OrderID")) set rsProd = Server.CreateObject("ADODB.Recordset")
12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
5
by: Mike D | last post by:
Attached is my code. Which I know there is a better way of writing however this is what I came up with and it works until the value is null or not = to the <> value. 'Do While rdrSQL.Read() ...
8
by: Steve Edwards | last post by:
Hi, While iterating through a multimap, I need to replace elements that meet certain conditions with a new element. There doesm't seem to be a replace() function for multimaps, so I'm inserting...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
7
by: RallyDSM | last post by:
Hello, I'm currently trying to read a .CSV file and get all the data into an array so I can work with it in the program. Here is what I currently have. Private Sub IntializeData() Dim AL...
5
by: manontheedge | last post by:
Sub Macro1() Dim num As Integer Dim scan As Integer Dim start As Integer Dim total As Integer start = 1
2
by: hexusnexus | last post by:
I wrote a simple algorithm and it keeps getting stuck in a loop. I guess I'm just to tired to figure it out: compcount= suitrank= trump=2 l,lt=0,0 while l<4: while lt<4:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.