java - 当进入某个case时如何停止?

标签 java netbeans

我正在处理一项基本上是工资日历的任务,其中输入员工信息然后进行计算。我将姓氏、名字和净工资存储到一个数组中,因此当它终止时,该数组将显示姓氏和净工资。我是java的初学者,并且已经尝试解决这个问题有一段时间了。

这是我的代码:

    /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cuyamacapayroll;
import java.util.*;
import java.util.Arrays;


class CuyamacaPayroll {

public static void main(String[] args) {    
Scanner sc = new Scanner(System.in);

    double grossWages, taxesWithheld, retirementWithheld, netWages, salary;
    String firstName, lastName;
    int typing;
    int numOfHours;        
        String employeeFirstName[] = new String[5];
        String employeeLastName[] = new String[5];
        double employeeNetWages[] = new double[5];

System.out.println("Cuyamaca Travel Agency Weekly Payroll Calculator");

int count = 0;
while(true) {
//Prompt the user for the employee type, followed by the employee name (first and last).
    System.out.print("Enter Employee Type [S]alaried, [H]ourly, [T]emporary, [Q]uit, [D]isplay Information: ");
    char typeOfEmployee = sc.next().charAt(0);
//After the employee name has been entered, you will use a switch statement
//to determine the next course of action dependent on the employee type.
//Your switch statement must be able to support both upper-case and lower-case characters,
//representing employee type. Depending on the employee type,
//you will prompt the user for either their yearly or hourly salary.

switch(typeOfEmployee) {

    case 's':
    case 'S':

        System.out.print("Enter Employee First Name: ");
              firstName = sc.next();
              employeeFirstName[ count ] = firstName;

        System.out.print("Enter Employee Last Name : ");
              lastName = sc.next();
              employeeLastName[ count ] = lastName;

        System.out.print(firstName + " " + lastName + "'s Yearly Salary: ");
              salary = sc.nextDouble();

        System.out.print("Enter " + firstName + " " + lastName + "'s Number of Hours: ");
              numOfHours = sc.nextInt();

        System.out.println("Employee: "+firstName+" "+lastName);
              grossWages = salary / 52;
              taxesWithheld = grossWages *.18;
              retirementWithheld = grossWages * .04;
              netWages = grossWages - taxesWithheld - retirementWithheld;
              employeeNetWages[ count ] = netWages;
                  count = count + 1; // moves the array to next index

        System.out.printf("Gross Wages: $%.2f\n", grossWages);
        System.out.printf("Taxes Withheld: $%.2f\n", taxesWithheld);
        System.out.printf("Retirement Withheld: $%.2f\n", retirementWithheld);
        System.out.printf("Net Wages: $%.2f\n", netWages);
              typeOfEmployee = 0;
break;
case 'h':
case 'H':
          System.out.print("Enter Employee First Name: ");
              firstName = sc.next();
              employeeFirstName[ count ] = firstName;

              System.out.print("Enter Employee Last Name : ");
              lastName = sc.next();
              employeeLastName[ count ] = lastName;


              System.out.print(firstName + " " + lastName + "'s Hourly Salary: ");
          salary = sc.nextDouble();

          System.out.print("Enter " + firstName + " " + lastName + "'s Number of Hours: ");
          numOfHours = sc.nextInt();

          System.out.println("Employee: "+firstName+lastName);
          grossWages = salary * numOfHours;
          taxesWithheld = grossWages *.18;
          netWages = grossWages - taxesWithheld;
          employeeNetWages[ count ] = netWages;

                  count = count + 1; // moves the array to next index
          System.out.println("Gross Wages: $"+grossWages);
          System.out.println("Taxes Withheld: $"+taxesWithheld);
          System.out.println("Net Wages: $"+netWages);
          break;
case 't':
case 'T':
          System.out.print("Enter Employee First Name: ");
              firstName = sc.next();
              employeeFirstName[ count ] = firstName;

              System.out.print("Enter Employee Last Name : ");
              lastName = sc.next();
              employeeLastName[ count ] = lastName;


              System.out.print(firstName + " " + lastName + "'s Hourly Salary: ");
          salary = sc.nextDouble();
          System.out.print("Enter " + firstName + " " + lastName + "'s Number of Hours: ");
          numOfHours = sc.nextInt();
          System.out.println("Employee: "+firstName+lastName);
          grossWages = salary * numOfHours;
          if(numOfHours > 35)
          grossWages += salary * (numOfHours - 35) * 0.5;
          System.out.println("Gross Wages: $"+grossWages);
          System.out.println("NO Taxes Deducted");
          break;

case 'd':
case 'D':  

    System.out.println("Display Employees: Sort By [L]ast Name, [W]ages or [X] To Quit: ");
    break;

case 'l':
case 'L':

    System.out.println("Last Names Are : " + Arrays.toString(employeeLastName));
    break;

case 'w':
case 'W':

    System.out.println("Net Wages Are : " + Arrays.toString(employeeNetWages));
    break;

case 'q':
case 'Q':
    return;

default  :  System.out.println("Invalid Employee Type");
}



}

}

          }

每当我输入“D”时,我希望它停止询问员工信息。

还有,有没有办法按字母顺序对数组进行排序?

如有任何帮助,我们将不胜感激!

最佳答案

不要使用 while(true) 有争议地运行 while 循环,而是使用 boolean 变量来控制循环。

将其声明为: boolean 检查 = true;

现在While条件是:while(check){ }

在 switch case 'D' 中,您想要终止,因此如果您输入 case 'D' 则将此 boolean 值设置为 FALSE。这样你就会跳出 while 循环。

希望这对您有帮助。

关于java - 当进入某个case时如何停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414552/

相关文章:

netbeans - 禁用 Netbeans 异常报告窗口

java - 使用eclipse 或netbeans 进行开发时,是否使用phpmyadmin 来管理mysql?

jakarta-ee - NetBeans 7.1 Java EE 中没有未经检查的警告

php - NetBeans 快捷方式的最终列表

即使不需要 xserver,Java 应用程序也会抛出 `Gtk-WARNING **: cannot open display:`

java - 从 stdin 读取但不知道何时停止

java - 如何阻止 Eclipse 在注释后移动我的行注释?

java - 基于另一个但具有不同 API 的 Swing 组件创建一个

java - 我可以为抽象类创建一个 DataOutputStream 方法并将其用于子类吗?

java - 堆空间内存不足错误