我制定了工资计划,我需要将其循环,以便为多名员工运行。
现在,它适用于一个人,但如果我在程序的顶部粘贴while(true){
,它会无限循环并永久打印消息。我对Java很陌生,所以任何建议都值得赞赏!
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
答案 0 :(得分:1)
您永远不会重置输入变量。因此,当循环返回到开始时,变量已经设置,程序会跳过验证检查。
例如,在循环开始时,我们设置iLength
和name
。说name="Alex"
,然后iLength=4
。这会通过while(iLength<3)
检查。然后输入其余数据(让我们假装)并完成循环。然后我们回到代码段while(iLength<3)
。由于我们尚未重置iLength
,iLength
仍为4,因此它甚至不会要求提供名称。
您需要在while(true)
循环中声明变量,这意味着它们会被销毁并重新创建每个循环,或者您需要在每个循环结束时重置它们。例如:
iLength=0;
name="";
值得注意的是,你应该有办法打破无限循环。 EG:
if (name.equalsIgnoreCase("exit")) return;
如果输入名称“exit”(任何案例变化),这将结束程序。
答案 1 :(得分:0)
您还可以使用for(;;) {}
创建一个无限循环,然后根据条件(运行次数/用户输入/等)创建break
。
答案 2 :(得分:0)
如果您知道有多少员工,那么请使用for循环。
如果您不知道有多少员工,那么您需要询问用户何时退出并在while循环中使用该答案,例如:
boolean goAgain = true;
while(goAgain){
//do your employee stuff
//ask user if they want to go again
goAgain = false; //assuming the user is finished
}
答案 3 :(得分:0)
这是以前所有答案的组合。可能需要一些边缘案例测试,但重置变量从一开始就重新开始工资。 do while只是我的偏好。
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 = "";
boolean again = true;
do{
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));
System.out.println("Do you want to enter another employee? (Y/N)");
if (sc.next().equalsIgnoreCase("n")){//left the exception handling up to you
again = false;
}
iLength =0;
inTime = 0;
outTime= 0;
PPH = 0;
}
catch(InputMismatchException ime){
System.out.println("Please enter time and pay as numbers.");
}
}while(again);
}