skylit

09-26-2011, 03:47 AM

I am doing practice problems for class (not homework), and I've gotten most of the coding down except for one stipulation. The exercise reads:

"Write a program that reads input of the number of hours an employee has worked and displays the employee's total and average (per day) number of hours.

The company doesn't pay overtime, so cap any day at 8 hours.

Sample Run:

How many days? 3

Hours? 6

Hours? 12

Hours? 5

Employee's total paid hours = 19

Employee’s average paid hours = 6.3333333"

I can't figure out how to include the bolded stipulation.

I've tried adding:

if ( sum >8 ) {

sum = 8;

}

but I know that isn't right, and I can't think of any other solution. Can someone advise me on my work?

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

System.out.println("How many days worked? ");

int count = keyboard.nextInt();

int i = 1;

int sum = 0;

while (i <= count) {

System.out.println("Hours? ");

sum += keyboard.nextInt();

i++;

}

System.out.println("Employee's total paid hours= " +sum);

System.out.println("Employee's average paid hours= " +(double)sum/count);

}

}

Also! If anyone can explain to me why there has to be an initialization of " int sum = 0 ", that would be great. I just know I have to include it for this loop.

"Write a program that reads input of the number of hours an employee has worked and displays the employee's total and average (per day) number of hours.

The company doesn't pay overtime, so cap any day at 8 hours.

Sample Run:

How many days? 3

Hours? 6

Hours? 12

Hours? 5

Employee's total paid hours = 19

Employee’s average paid hours = 6.3333333"

I can't figure out how to include the bolded stipulation.

I've tried adding:

if ( sum >8 ) {

sum = 8;

}

but I know that isn't right, and I can't think of any other solution. Can someone advise me on my work?

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

System.out.println("How many days worked? ");

int count = keyboard.nextInt();

int i = 1;

int sum = 0;

while (i <= count) {

System.out.println("Hours? ");

sum += keyboard.nextInt();

i++;

}

System.out.println("Employee's total paid hours= " +sum);

System.out.println("Employee's average paid hours= " +(double)sum/count);

}

}

Also! If anyone can explain to me why there has to be an initialization of " int sum = 0 ", that would be great. I just know I have to include it for this loop.