我的程序应该显示工人总薪水的计算,但不管怎么说,在包括程序结束后,所有输入都没有显示任何结果。
还要检查输入的总小时数是否超过40,如果是,则计算加班工资。
public class GajiTest {
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
class Salary {
public double CalculateGaji(int hours,int rate){
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
}}}
答案 0 :(得分:1)
我想问题是添加两行试试这个:
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
Salary sal=new Salary();
sal.CalculateGaji(hours,rate,empName);
}
}
}
class Salary {
public void CalculateGaji(int hours,int rate,String empName){
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
答案 1 :(得分:0)
为什么不打印任何内容是因为您没有调用CalculateGaji()
的方法class Salary
。
您需要通过创建类Salary
的对象来调用它。 (在主方法中的本地类之后。)
Salary salary = new Salary();
salary.CalculateGaji(hours,rate);
除此之外,还有一些我想说的话,
您正在创建一个整数数组int[] arrayList= new int[numberOfEmp];
ArrayList是另一回事。 What is an ArrayList?不应该将变量的名称写为arrayList
。在某个阶段可能会让你感到困惑。
根据您的代码,您已在class Salary
中创建了课程main method
。这样它就变成了[Local Class][2]
。如果这是你想要的,那就找到了。否则,您可以编写class Salary
外侧主要方法和课外class GajiTest
。
但我认为没有必要创建任何本地类来实现它。您可以通过以下方式直接执行此操作,
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
答案 2 :(得分:0)
从您的代码中,我认为您要做的是创建一系列员工并打印出他们的姓名和薪水。如果您愿意,代码中存在多个错误,无法使其正常运行。
以下是创建员工阵列并根据您的CalculateGaji函数打印出姓名和薪水的代码:
public class calculateSalary {
static class employee{
private double rate;
private double hours;
private String empName;
public employee(){}
public void setRate(double value){
rate = value;
}
public void setHours(double value){
hours = value;
}
public void setName(String value){
empName = value;
}
public void CalculateGaji(){
if (hours >=40){
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName +"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName + "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
employee[] employeeArray= new employee[numberOfEmp];
for (int i = 0; i < employeeArray.length; i++){
employeeArray[i] = new employee();
System.out.println("Enter Employee Name: ");
employeeArray[i].setName(input.next());
System.out.println("Enter hourly rate: ");
employeeArray[i].setRate(input.nextDouble());
System.out.println("Enter hours worked: ");
employeeArray[i].setHours(input.nextDouble());
}
for(int i = 0; i < employeeArray.length; i++){
employeeArray[i].CalculateGaji();
}
}
}
希望这有帮助。