Java 简单的薪资计划

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18284082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:03:00  来源:igfitidea点击:

Simple Payroll Program

java

提问by Bob B.

Modify the Payroll Program application so it continues to request division information until the user enters stop as the division name. In addition, program the application to check that the number of employees and average salary per employee are positive numbers. If either the number of employees or the average salary per employee is not a positive value, the application should prompt the user to enter a positive amount.

修改薪资计划应用程序,使其继续请求分部信息,直到用户输入 stop 作为分部名称。此外,编写应用程序以检查员工人数和每位员工的平均工资是否为正数。如果员工人数或每位员工的平均工资不是正值,应用程序应提示用户输入正数。

This is the code I have come up with, which compiles fine but doesn't run the way I expect it to.

这是我想出的代码,它编译得很好,但没有按照我期望的方式运行。

import java.util.Scanner; //program uses class SCanner

public class PayrollPart2
{

    public static void main( String[] args ) 
    {
        Scanner input = new Scanner( System.in ); // create Scanner to obtain input from command window

        // variables
        char name; // divisions's name
        int number1; // number of employees in the division
        double number2; // average salary for the employees
        double product; // total division payroll

            //prompt user to input division name
            System.out.print( "Enter Division's name, type stop to exit: ");
            String divisionName = input.nextLine(); //read line of text
                while (divisionName !="stop") 
                {
                //prompt user for number of employees in the division
                System.out.print( "Enter the number of employees in the division: ");
                //read number of employees from user's input
                number1 = input.nextInt();
                    while (number1 <= 0)
                    {
                        System.out.print("Please enter a positive number of employees in the division:"); // prompt
                        number1 = input.nextInt(); // input
                    }

                //prompt user to enter average salary of employees
                System.out.print("Enter average salary for the employees: " );
                //read average salary
                number2 = input.nextDouble();
                    while (number2 <= 0)
                    {
                        System.out.print("Please enter a positive number for the employees salary:"); // prompt
                        number2 = input.nextDouble(); // input
                    }

                //multiply Number1 by Number2
                product = number1 * number2;

                //displays division and total division payroll
                System.out.printf( "The division %s has a payroll of $%.2f\n" , divisionName, product );
                }
    } //end method main
} // end class PayrollPart2

I've been at it for about 8 hours and at this point I am completely lost on the next steps. The code goes through the loop but doesn't ask for a different division name. Typing the exit command on the first prompt doesn't actually exit the loop. Should I be using If/Else statements instead? I think I can use a Boolean but I'm not exactly sure how to implement it.

我已经做了大约 8 个小时,此时我完全迷失在接下来的步骤中。代码通过循环但不要求不同的部门名称。在第一个提示符下输入 exit 命令实际上并没有退出循环。我应该改用 If/Else 语句吗?我想我可以使用布尔值,但我不确定如何实现它。

回答by Juned Ahsan

Use string equals()method to do the string comparison instead of != or ==. One of the problem in your code is this statement:

使用 string equals()方法进行字符串比较,而不是 != 或 ==。您的代码中的问题之一是以下语句:

while (divisionName !="stop") 

change it to

将其更改为

while(!("stop".equals(divisionName)))

!= will chech whether the two objects are not the same memory object, while equals method will do the string contents comparison.

!= 将检查两个对象是否不是同一个内存对象,而 equals 方法将进行字符串内容比较。

Also note the reverse comparision of string "stop" against the divisionName. This will help to avoid null pointer exception in case divisionName is null.

还要注意字符串 " stop" 与divisionName. 这将有助于避免在 DivisionName 为空的情况下出现空指针异常。

Learn more about string comparison from a related post: Java String.equals versus ==

从相关帖子中了解有关字符串比较的更多信息:Java String.equals 与 ==