java - 运行时忽略 While 循环?

标签 java netbeans while-loop

我是论坛新手,所以首先我想说“嗨”!我是 Java 编程新手,正在尝试使用三个 while 循环制作一个简单的工资计算程序。

第一个 while 循环使程序继续运行,直到用户输入标记“停止”。第二个和第三个循环是错误陷阱,确保用户在继续之前输入正数。

由于某种原因,while 循环不起作用,我已经尝试了我能想到的所有变体。程序运行得很好,只是忽略了 while 循环。如果有人可以就我做错的事情提供一些建议,我将非常感激。

如果有帮助的话,我正在使用 NetBeans 8.0 IDE。

这是我的代码:

Import java.util.*;

Import java.text.*;

public class PayrollProgramVersion2 
{

    //begin main program
    public static void main(String[] args) 
    {
        //declare new scanner
        Scanner sc = new Scanner (System.in); // declare new scanner object
        DecimalFormat Dollars = new DecimalFormat ("$0.00"); //format for dollars
        String Employee; //employee's name
        Double Hours, //hours worked
        Rate, //pay rate
        Pay; // Hours * Rate
        Boolean Continue = true; // sentinel for program loop
        //welcome user, prompt for employee name, and assign input to Employee
        System.out.println ("Welcome to the payroll program!");
        System.out.println ("What is the employee's name? (Enter stop to quit.)");
        Employee = sc.nextLine();
        // while loop continues program until user enters "stop"
        while (Continue == true) 
        {
            if (Employee.equalsIgnoreCase("stop"))
            {
                Continue = false;
            } // end if
            else
            {
                //prompt for hours worked and assign to Hours
                System.out.println ("How many hours did " +Employee+ " work?");
                Hours = sc.nextDouble();
                //this block is an error trap to ensure input is positive before continuing
                while (Hours < 0)
                {
                    System.out.println( "Error - input must be a positive number");
                    System.out.println ("How many hours did " +Employee+ " work?");
                    Hours = sc.nextDouble();
                }
                //prompt for pay rate and assign to Rate
                System.out.println( "How much does " +Employee+ " make per hour?");
                Rate = sc.nextDouble();
                //this block is an error trap to ensure input is positive before continuing
                while (Rate < 0)
                {
                    System.out.println( "Error - input must be a positive number"); 
                    System.out.println( "How much does " +Employee+ " make per hour?");
                    Rate = sc.nextDouble();
                }
                Pay = Hours * Rate; // calculate payrate
                //display results
                System.out.println(Employee+ "'s paycheck is " +(Dollars.format(Pay))+ ".");
                System.out.println ("What is the employee's name? (Enter stop to quit.)");
                Employee = sc.nextLine();
            } //end else
        } //end while
        System.out.println ("Thank you for using the payroll program. Goodbye!");
    } // end main

} // end program

最佳答案

据我所知,您应该将 while (hours<0) 更改为 while (hours<0 || hours == null)

这是因为...据我所知,您初始化了时间。但没有向其中输入任何值。所以它仍然是 null 。您也可以尝试将 while 更改为 if。

希望这有帮助。它可能默认为 0,但出于测试目的,有控制台输出可能是值得的。

System.out.println(hours);

在 while 循环之前查看您的程序将 hours 读取为什么。

希望这有帮助。

关于java - 运行时忽略 While 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731302/

相关文章:

Java 文件写入和 Raspberry Pi

java - 从 3.0 版本开始,Quartz 调度程序不再对 Spring 有用吗?

java - 为什么我的代码可以在某些网站上运行,但不能在 NetBeans 中运行?

c++ - boost 线程链接在 Netbeans 7.1 调试/测试 session 中失败

php - 带有 "offset"包装的循环(while/foreach)

php - mysql while循环的最后一个值

java - 剪刀石头布的简单java使用方法

java - 如何使用 sbt 布局从 scala 源访问资源目录中的文件

java - Netbeans 7.2.1。带有嵌入式 Derby 数据库的 Java 应用程序

c++ - 带有 try catch 的循环在错误的 cin 输入时失败