修改 Payroll Program 应用程序,使其继续请求部门信息,直到用户输入 stop 作为部门名称。此外,对应用程序进行编程以检查员工人数和每位员工的平均工资是否为正数。如果员工人数或每位员工的平均工资不是正值,应用程序应提示用户输入正数。
这是我想出的代码,它编译得很好,但没有按照我期望的方式运行。
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
我已经做了大约 8 个小时,此时我完全迷失在接下来的步骤中。代码通过循环,但不要求不同的部门名称。在第一个提示符下键入 exit 命令实际上并没有退出循环。我应该改用 If/Else 语句吗?我想我可以使用布尔值,但我不确定如何实现它。