• November 3rd, 2013, 03:49 PM
gotdatdough
My program is supposed to calculate the income of the user by taking the number of hours worked and their hourly wage. if the person works over 40 hours, thy recieve 1.5 times the amount of their usual hourly wage. for some reason, when 41 hours is entered with \$10 per hour, the program outputs \$425 when it should output \$415. got any ideas why??
Code Java:

```import java.util.Scanner;   public class WageCalculator {   public static void main(String[] args) { Scanner scan = new Scanner(System.in); double hoursworked=0, hoursworked40 = 0, wage, totalpay; int i = 0; System.out.println("Enter number of hours worked: "); hoursworked = scan.nextInt(); System.out.println("Enter hourly wage: "); wage = scan.nextInt(); while (hoursworked >= 0 && hoursworked <= 168 && i < 1){ if(hoursworked > 40){ hoursworked40 = hoursworked - 40; hoursworked40 = hoursworked40 * 1.5; totalpay = (hoursworked + hoursworked40) * wage; System.out.println("Total pay: \$" + totalpay); i+=1; }else{ totalpay = hoursworked * wage; System.out.println("Total pay: \$" + totalpay); i+=1; } } if(hoursworked < 0 || hoursworked > 168) System.out.println("Your hours need to be between 0 and 168."); } }```
• November 3rd, 2013, 04:02 PM
Norm
Quote:

got any ideas why??
Which line(s) of code is computing the wrong value?
Add a println() after each line that assigns a value to a variable and print out the variable's value.
If you know how the computations are supposed to go, you should be able to look at the output and see where the problem is.
• November 3rd, 2013, 04:14 PM
"totalpay = (hoursworked + hoursworked40) * wage;"
In that line it calculates the extra hour both in hoursworked(41) and hoursworked40(1.5). You should make "hoursworked=40;" before that line.
• November 3rd, 2013, 04:35 PM
gotdatdough
Quote:

Originally Posted by Norm
Which line(s) of code is computing the wrong value?

Code Java:

```hoursworked40 = hoursworked - 40; hoursworked40 = hoursworked40 * 1.5; totalpay = (hoursworked + hoursworked40) * wage;```
These lines are.

--- Update ---

Thank you. i knew it was somehting simple!
• November 3rd, 2013, 04:45 PM
Norm
Quote:

These lines are.
Did you print out the values computed and assigned to see where the problem is?
What values should each of those expressions compute?
What values did each of those expressions compute?

It's nice that someone else did your work and figured it out for you. But you need to learn how to find and fix problems yourself.