有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有没有一种方法可以在程序完成时循环我的程序?我总是得到一个无限循环

我制定了一个工资计划,我需要循环它,这样它可以为多个员工运行

现在,它只适合一个人,但如果我在程序顶部粘贴一个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

共 (4) 个答案

  1. # 1 楼答案

    您还可以使用for(;;) {}创建一个无限循环,然后根据条件(运行次数/用户输入/etc)仅break

  2. # 2 楼答案

    下面是前面所有答案的组合。可能需要一些边缘案例测试,但重置变量会从一开始就重新启动工资。我喜欢边做边做

    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);
    }
    
  3. # 3 楼答案

    您永远不会重置输入变量。因此,当循环回到开始时,变量已经设置好,程序跳过验证检查

    例如,在循环开始时,我们设置iLengthname。说name="Alex",然后说iLength=4。这通过了while(iLength<3)检查。然后输入其余数据(让我们假装),循环完成。然后我们回到代码段while(iLength<3)。因为我们没有重置iLength,所以iLength仍然是4,因此它甚至从不要求名称

    您要么需要在while(true)循环中声明变量,这意味着每个循环都会销毁并重新创建变量,要么需要在每个循环结束时重置变量。例如: iLength=0; name="";

    同样值得注意的是,您应该有一种打破无限循环的方法。例如:

    if (name.equalsIgnoreCase("exit")) return;

    如果输入名称“退出”(有任何大小写变化),则程序将结束

  4. # 4 楼答案

    如果您知道有多少员工,则使用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
    }