私は基本的に従業員情報が入力され、計算される給与計算カレンダーである課題に取り組んでいます。私は姓、名、およびネット賃金を配列に格納したので、それが終了すると配列に姓と正味の賃金が表示されます。私はJavaの初心者ですし、しばらくの間これを修正しようとしています。ここで特定のケースに入ると停止する方法は?
は私のコードです:私は「D」を入力するたびに
/*
* 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");
}
}
}
}
は、私はそれが従業員情報を求めて停止したいです。
また、配列をアルファベット順にソートする方法はありますか?
ご協力いただきありがとうございます!
お返事ありがとうございます。私は間違った言葉を綴っていることに気づき、それを修正しました。 私はfalse = trueを試しました。予期しないタイプのエラーを私に与えましたが、フラグ= trueに変更しました。エラーはありませんが、修正はありませんでした。助言がありますか? – JohnnyB
while(!flag)とflag = true ...である必要があります。回答を更新しました。コメントありがとうございました –
こんにちは、ありがとう。私がDに入るたびに、ループではなくプログラム全体が終了します。 – JohnnyB