[Solved] How can i used nested while loop ? in java


In your outer while loop you need this:

overtime = 0;
if (hoursWorked>8)
    overtime = (hoursWorked-8)*(rateOfPay*1.5) - (hoursWorked-8)*rateOfPay;        // Calculate OVERTIME HOURS

sets the overtime to zero for each iteration, and you’ll have to subtract the normal rate for overtime hours (otherwise you add them twice)

and then you’ll have to add the overtime to the wages:

wages += (hoursWorked * rateOfPay) + overtime;

Put it all in a do-while loop and set the day=1 at the start, like this:

String choice = "Y";
do {
    day=1;
    while (day <= 5) // on weekday
...

and at the end:

...
    } //end while loop for weekend.

    choice = JOptionPane.showInputDialog(null, "Do you want to calculate another weeks wages -Yes or No?", "TRY AGIN", JOptionPane.PLAIN_MESSAGE);

} while (choice.equals("Y") || choice.equals("y"));
JOptionPane.showMessageDialog(null, "Pay this week is £" + wages, "Pay", JOptionPane.PLAIN_MESSAGE);

To your extra question -´at the start:

double totalWages = 0;
String choice = "Y";
do {
    day=1;
    wages = 0;
...

and at the end:

...
    JOptionPane.showMessageDialog(null, "Pay this week is £" + wages, "Pay", JOptionPane.PLAIN_MESSAGE);
    choice = JOptionPane.showInputDialog(null, "Do you want to calculate another weeks wages -Yes or No?", "TRY AGIN", JOptionPane.PLAIN_MESSAGE);
    totalWages += wages;

} while (choice.equals("Y") || choice.equals("y"));
JOptionPane.showMessageDialog(null, "Pay total is £" + totalWages, "Pay", JOptionPane.PLAIN_MESSAGE);

2

solved How can i used nested while loop ? in java