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]
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]
Comment