嵌套if/else语句的while循环

3

你好,我遇到了程序无法正常运行的问题。我已经排除了所有语法错误,但现在我的输出有问题。

首先,在第一个IF语句中,它同时提示用户输入姓名和部门,所以当它输出时,姓名为空,只有部门有输入。我认为这与整个IF语句有关,因为如果我将“String name”更改为input.next,那么姓名就会正确提示,但dept和totalHrsWkd会合并在一起。

此外,在测试程序时,如果我输入负数的totalHrsWkd,它会崩溃。它会显示两个print语句都在一行上,然后JCreator会崩溃。

我会感激任何帮助,谢谢!

    public static void main(String[] args)
    {
    // TODO code application logic here

    int attempt = 1, employeeID = 0;
    double hoursWorked = 0.0;
    double overtimeHrs = 0.0;
    double totalHrsWkd = 0.0;

    Scanner input = new Scanner(System.in);

    while( attempt < 4 )
    {
        System.out.println( "Enter your employee ID: " );
      employeeID = input.nextInt();

        if( employeeID == 12345678 )
        {
        System.out.printf( "Enter your name: " );
        String name = input.nextLine();

    System.out.printf( "Enter your department: " );
        String dept = input.nextLine();

    System.out.printf( "Enter your hours worked including overtime:  " );
        totalHrsWkd = input.nextDouble();

        while( totalHrsWkd < 0 )
            {
            System.out.printf( "Try again!  Hours worked cannot be negative.");

            System.out.printf( "Enter your hours worked including overtime: ");
            }

            overtimeHrs = totalHrsWkd - 40;
            hoursWorked = totalHrsWkd - overtimeHrs;

            if( overtimeHrs <= 0 )
            {
            }
            else if( overtimeHrs == 0 )
            {
            }
            else if( hoursWorked == totalHrsWkd )
            {
            }
            else if( hoursWorked == 40 )
            {
            }
        System.out.printf( "Name: %s\n" + "Dept: %s\n" + 
                    "Hours Worked:  %.2f\n" + "Overtime Hours: %.2f\n"
                     + "Total Hours Worked: %.2f\n", name, dept, 
                     hoursWorked, overtimeHrs, totalHrsWkd);

        attempt = 3;
        }
    else
    {
        if(attempt < 3)
        {
        System.out.println( "Invalid ID!  Try again."  );
        }
        else
        {
        System.out.println( "Invalid ID!  0 attempts left.  Exiting program!" );
        }

      }

    ++attempt;
   }
    System.exit(0);
}
}

totalHrsWkd = input.nextDouble(); 应该放在 while 循环内。否则你只是在一个无限循环中,而 totalHrsWkd 没有发生任何变化。 - biziclop
你为什么选择使用 Scanner?这在交互式程序中几乎从不合适(并会导致像你在这里看到的问题)。相反,使用 BufferedReader.readLine - Greg Hewgill
是的,这是我Java 1课程的编程作业。基本上老师提供了伪代码,我们需要编写程序。到目前为止,我们只涉及了Scanner类。在我们的书中,我甚至还没有接触过BufferedReader.readLine。 - Abweichung
@GregHewgill 我猜这是因为在大学里处理用户输入时,Scanner 是你学习的第一件事情。 - user855520
这仍然不适合交互式程序。 - Greg Hewgill
3个回答

4
问题在于:
  employeeID = input.nextInt();

从输入流中读取下一个数字序列。然而,为了实际输入员工ID,您还需要按下Enter键。 Enter键的按下仍在等待输入,因此当您下一次调用时,

    String name = input.nextLine();
Scanner类会自动响应用户按下回车键,此时程序会默认用户想要输入一个空名称。因此,你甚至没有机会输入名称,这就是为令人困惑的地方,看起来好像程序同时询问了两个问题(名称和部门)。
如果您希望继续使用Scanner类,则建议避免使用nextInt()等方法,改用nextLine()方法。当然,您需要使用例如parseInt()将用户输入的数字转换为Java整数。

那我需要先将其转换为字符串吗?例如:String ID = System.out.println( "Enter your employee ID: "); int employeeID = Int.parseInt( ID );如果是这样,我会遇到不兼容的类型错误以及找不到符号变量Int的问题。 - Abweichung
不太对,您试图将println()的返回值赋给一个String。请尝试:String employeeIDStr = input.nextLine(); int employeeID = Integer.parseInt(employeeIDStr); 还要注意使用Integer类而不是Int - Greg Hewgill
终于!非常抱歉给你带来初学者问题的麻烦,但基本上我只能自力更生,因为她会花课堂时间讲解幻灯片。现在我必须从头开始编写程序,所以我想先完成这个程序,以便作为模板。再次感谢! - Abweichung

2
attempt = 3; // Seems suspicious

attempt++; // I think it's more appropriate in loops of this kind than ++attempt

1
你的程序在totalHrsWkd小于0时无法工作的原因是下面的代码创建了一个无限循环。
  while ( totalHrsWkd < 0 )
            {
            System.out.printf( "Try again!  Hours worked cannot be negative.");

            System.out.printf( "Enter your hours worked including overtime: ");
            }

你也应该在这个循环中读取工作小时数。

现在我明白上面所说的是什么了,缺少totalHrsWkd = input.nextDouble(); 我需要将它放到我的第二个提示下面。这解决了我的无限循环问题,但我还在处理另一个问题,谢谢! - Abweichung

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接