I made a wages program and I need to loop it so it runs for multiple employees. Right now it works fine for one person but if I stick a while(true){ at the top of the program, it infinite loops and prints messages forever. I am rather new to java so any suggestions are appreciated!
import java.util.*;
import java.text.*;
public class Wages{
public static void main (String[ ] arg){
DecimalFormat money = new DecimalFormat("$0.00");
int inTime = 0;
int outTime = 0;
int inMin = 0;//convert time to minutes for easier calculation
int outMin = 0;
int iLength = 0;
double minWorked = 0;
double hoursWorked = 0;
double totalPay = 0;
double PPH = 0; //pay per hour
Scanner sc = new Scanner(System.in);
String name = "";
String timeIn = "";
while(true){
while(iLength<3){ //loop if input is invalid
System.out.print("Please enter your name: ");
name = sc.nextLine( );
name = name.trim( ); //trim spaces
iLength = name.length( );//check length
if(iLength<3){//error message
System.out.println("Please enter a name longer than 3 characters.");
}
}
try{
while((inTime<800 || outTime>1900)||outTime<=inTime){
System.out.print("Please enter your check in time: ");
inTime = sc.nextInt( );
System.out.print("Please enter your check out time: ");
outTime = sc.nextInt( );
if(inTime<800||outTime>1900){
System.out.println("Please enter work time between 800 and 1900.");
}
if(outTime<=inTime){
System.out.println("Please enter a check out time later than your check in time.");
}
}
while(PPH<7.75||PPH>15.20){
System.out.print("Please enter your pay per hour: $");
PPH = sc.nextDouble( );
if(PPH<7.75||PPH>15.20){
System.out.println("Please enter a pay between 7.75 and 15.20.");
}
}
inMin = (inTime/100)*60 + (inTime%100);
outMin = (outTime/100)*60 + (outTime%100);
minWorked = outMin - inMin;
hoursWorked = minWorked/60;
totalPay = hoursWorked*PPH;
System.out.println("\nEmployee Name: " + name);
System.out.println("Check in time: " + inTime);
System.out.println("Check out time: " +outTime);
System.out.println("Hours worked: " + hoursWorked);
System.out.println("Pay per hour: " +money.format(PPH));
System.out.println("Total pay: " + money.format(totalPay));
}//try
catch(InputMismatchException ime){
System.out.println("Please enter time and pay as numbers.");
}
}//while(true)
}//main
}//class
Aucun commentaire:
Enregistrer un commentaire