Hi all,
I have a program that I have to write with an output that looks like this:

----------------------------------------------------------
Wages for 3 employees

For 10 hours worked, the wages are 120 dollars
For 20 hours worked, the wages are 240 dollars
For 30 hours worked, the wages are 360 dollars
For 40 hours worked, the wages are 480 dollars

Wages for 4 employees

For 10 hours worked, the wages are 160 dollars
For 20 hours worked, the wages are 320 dollars
For 30 hours worked, the wages are 480 dollars
For 40 hours worked, the wages are 640 dollars

Wages for 5 employees

For 10 hours worked, the wages are 200 dollars
For 20 hours worked, the wages are 400 dollars
For 30 hours worked, the wages are 600 dollars
For 40 hours worked, the wages are 800 dollars

Wages for 6 employees

For 10 hours worked, the wages are 240 dollars
For 20 hours worked, the wages are 480 dollars
For 30 hours worked, the wages are 720 dollars
For 40 hours worked, the wages are 960 dollars

-------------------------------------------------------

my program however only outputs:

------------------------------------------------------
Wages for 3 employees

For 10 hours worked, the wages are 120 dollars
For 20 hours worked, the wages are 240 dollars
For 30 hours worked, the wages are 360 dollars
For 40 hours worked, the wages are 480 dollars

Wages for 4 employees

Wages for 5 employees

Wages for 6 employees

---------------------------------------------------------

How do I fix this?

Heres the code:

import java.util.Scanner;
import java.io.*;
public class prog166d2
{
    public static void main (String[] args)
    {
      int hours = 10;
      int wage = 4;
      int emp = 3;
      int maxemp = 6;   //maximum number of employees
      int count = 0;
      int counter = hours * wage;
      while(count <= (maxemp-emp)){
      while(emp <=maxemp)
      {

      System.out.println("\nWages for " +emp +" employees\n");

      while(hours<= counter)
      {
      int calc = hours * wage * emp;
      System.out.println("For " +hours +" hours worked, the wages are " +calc +" dollars");
      hours +=10;


     }

    while(hours ==counter)
    {hours = 10;}
     emp += 1;}

     count +=1;
    }
    }
}

Thanks!

Recommended Answers

All 3 Replies

The first step in solving this problem, I think, is learninig to indent your code in a consistent and legible manner; since this is Java, I recommend following the official Java Style Guide, at least as far as the basics are concerned.

import java.util.Scanner;
import java.io.*;

public class prog166d2 {
    public static void main (String[] args) {
        int hours = 10;
        int wage = 4;
        int emp = 3;
        int maxemp = 6;   //maximum number of employees
        int count = 0;
        int counter = hours * wage;

        while(count <= (maxemp - emp)) {
            while(emp <= maxemp) {

                System.out.println("\nWages for " + emp + " employees\n");

                while(hours <= counter) {
                    int calc = hours * wage * emp;
                    System.out.println("For " + hours + " hours worked, the wages are " + calc + " dollars");
                    hours += 10;
                }

                while(hours == counter) {
                    hours = 10;
                }
                emp++;
            }
            count++;
        }
    }
}

Once you've done that, you'll see that your outermost loop is not actually necessary - you only need the first inner loop and the loop inside of that one. The test for hour being equal to counter is also unneeded - if it reaches that point, it is always going to be equal to counter, while on the second pass it will always be unequal. I would add that counter itself is unneeded, as it will always equal 40, though it will be different if you ever change wage; there's no actual relationaship between the maximum number of hours, so this is a misleading and potentially incorrect statement. You'd be better off redeclaring it as a final value simply as 40:

final int maxHours = 40;

Also, I think you would do well to redesign the code to use for() loops rather than while() loops, though if the assignment is specifically about while() loops, there's not much you can do about that.

Line 28, hours will never be equal to counter. The hours is incremented higher than counter before it can get out of the loop, so the hours variable will never be reset. The problem is that you excessively use while loop even though you do not need to. As a result, your while-loop causes the bug. Anyway, Schol-R-LEA has already pointed out your big problem.

Thanks for the help guys!
I finally got it to work after some heavy editing XD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.