0

I've been trying to write a java program to calculate daily salary relying on hours worked per day. and if it was weekend or no. (boolean value).

The time the user need to write is 0824 it equals to 08:24 (no idea how to ask for hour:minutes pattern and subtract them).

I've been using only one loop to ask for payment Per Hour again and again if the user puts values lower than 28.00$ or higher than 100.00$.

When the program running after asking for paymentPerHour the program stops <terminated> is not shown.

Would appreciate any help. Thanks! :) (don't pay attention to the class name)

package ouzanFirstProject;

import java.util.Scanner;

public class convertDecToBinary {

    public static void main(String[] args) {
        Scanner in= new Scanner(System.in);
         int paymentPerHour , entryHour = 0 , exitHour = 0;
         float totalHour= (exitHour-entryHour)/100;
         boolean workedAtWeekend;
         float salary = 0;
         
        System.out.println("Please enter the hour you arrived to work (HHMM)");
        entryHour=in.nextInt();
        System.out.println("Please enter the hour you exit from work(HHMM)");
        exitHour=in.nextInt();
        System.out.println("Please enter payment per hour (between 28.00 and 100.00):");
        paymentPerHour=in.nextInt();
        while(paymentPerHour>0) {
            if(paymentPerHour<28 ||paymentPerHour>100) {
            System.out.println("Please enter payment per hour");
            paymentPerHour=in.nextInt();
            }
                if(paymentPerHour>=28 &&paymentPerHour<=100) {
                continue;
                }
        }
        System.out.println("Did you work on weekend ? (True/False)");
        workedAtWeekend=in.hasNext();
        if(workedAtWeekend){
            salary= (float) ((totalHour)*0.20+100);
        }
        else if (totalHour>=9) {
            salary=(float) ((float)(totalHour)*paymentPerHour*1.5);
            if(totalHour>11) {
                salary = (float)((float)(totalHour)*paymentPerHour*2);
            }
        }
        else if(totalHour<9) {
            salary=(float)((float)(totalHour)*paymentPerHour*0.1);
            if(totalHour<=1) {
                salary= 0;
            }
                if(totalHour>=15) {
                    System.out.println("You cant work more than 15 hours a day");
                }
                    if(totalHour<0) {
                        salary=Math.abs(totalHour)*paymentPerHour;
                    }
        }
        System.out.println("You've been working for: "+totalHour+" Hours"+",And your payment is: "+salary);
    }

}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tal Ouzan
  • 75
  • 1
  • 5
  • `continue;` will continue your loop and jump to the next iteration. So once your paymentPerHour is in the valid range (>=28 <=100) you always tell your while loop to do the next iteration. Did you by any chance mean to use `break;` to break the loop? – OH GOD SPIDERS Dec 16 '19 at 18:31
  • `while(paymentPerHour>0)` ... so it's okay if they enter a **negative** number?! – Idle_Mind Dec 16 '19 at 18:34
  • after fixing the `continue` problem, check [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/a/13102066/85421) – user85421 Dec 16 '19 at 19:06
  • Hi everyone , thanks for answer. tried to add continue; inside the loop. still the same result. (the program doesn't continue without errors). – Tal Ouzan Dec 16 '19 at 20:33
  • As a side note, I would avoid using `float` or `double` for calculations including money since they are inherently limited in precision. Use `BigDecimal` instead. – Bernard Aug 10 '20 at 06:48

1 Answers1

0

You should not use continue; in the loop. It will force the loop to go to the next iteration even if the conditions are met. Instead you should use break; which will terminate the loop if the conditions are met. Also you should not add the condition paymentPerHour>0 because it will skip the loop if the user enters a negative number (thx to Idle_Mind for this one). See the code sample:

while(true)
{      
      if(paymentPerHour>=28 &&paymentPerHour<=100) {
         break; // This will terminate the loop if the conditions are met
       }

    // This part will only run if the conditions are not met
      System.out.println("Please enter payment per hour");
      paymentPerHour=in.nextInt();
}