Khovsepa - 1 year ago 93
Java Question

# Having trouble with count controlled for loop

so I'm taking an Intro to Java class and am having a bit of difficulty finishing up my homework.

First, I'm going to paste the homework directions and then I'll post my code so you all can see where I'm struggling and hopefully be able to help me out.

DIRECTIONS:
For the first day worked a person earns \$1.00 for the entire day. For the second day worked the person's daily pay is doubled to \$2.00. For the third day worked the person's daily pay is doubled again to \$4.00. For each consecutive day a person works their daily pay is doubled in this way.

Write a program that asks a user to enter the number of days they worked and then calculates the pay they earned for each day along with the total pay they earned for all of the days. The program output should be the pay for each day and the total pay earned for all the days.

Input Validation: Do not let the user enter a number of days that is less than 1. Use a loop to prompt them to enter another number for the number of days if they enter a number that is less than 1. Make sure and format your output.

MY CODE:

``````import java.util.Scanner;

import java.text.DecimalFormat;

public class Homework7Pennies

{

public static void main (String[] args)

{
Scanner keyboard = new Scanner (System.in);
DecimalFormat formatter = new DecimalFormat("\$#0.00");
double totalPay = 0;
int totalDays;

System.out.println("Please enter the number of days you worked: ");
totalDays = keyboard.nextInt();

while (totalDays <1)
{
System.out.println("You have entered an invalid number of days. ");
System.out.println("Please enter the number of days you worked: ");
totalDays = keyboard.nextInt();
}

for(int counter = 1; counter <= totalDays; counter++)
{
System.out.println("Pay for Day #" + counter + ": " + formatter.format(counter));
}
totalPay = totalPay + counter;
System.out.println("TOTAL PAY FOR " + totalDays + " DAYS: " + formatter.format(totalPay));

}
}
``````

The issues I am having is:
1) I'm not sure how to double the pay without doubling the first days pay as well

2) How would I print the TOTAL PAY just one time? As of right now, whenever I run my program, it'll print it the same number of times as the number of days worked. I only need it to print once.

This is what you want to do.I just made small changes in you `for` loop.And putting `totalPay = totalPay + result` inside loop.Hope this will help you.

``````public class Test {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("\$#0.00");
double totalPay = 0;
double result = 1.00;//for showing per day payment
int totalDays;

System.out.println("Please enter the number of days you worked: ");
totalDays = keyboard.nextInt();

while (totalDays < 1) {
System.out.println("You have entered an invalid number of days. ");
System.out.println("Please enter the number of days you worked: ");
totalDays = keyboard.nextInt();
}

for (int counter = 1; counter <= totalDays; counter++) {
System.out.println("Pay for Day #" + counter + ": " + formatter.format(result));
totalPay = totalPay + result;//for getting total payment
result *= 2;//for doubling payment as number of day increase
}

System.out.println("TOTAL PAY FOR " + totalDays + " DAYS: " + formatter.format(totalPay));
}

}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download