gpt4 book ai didi

java - 将二维数组传递给构造函数

转载 作者:行者123 更新时间:2023-11-30 11:00:36 26 4
gpt4 key购买 nike

你能告诉我如何将二维数组传递到构造函数中吗?我已经传递了带有值的数组,但我得到了错误的答案。我在这里分享我的代码。

    package payroll;
import java.util.Scanner;
import javax.swing.*;

class Payroll
{
private String empID;
private String empName;
private int numOfHours;
private double hourlyRate;
private double totalPay;

public Payroll(){}

public Payroll(Object[][]emp, int scale)
{
for(int k=0;k<emp.length;k++)
{
this.empID = emp[k][l].toString();
this.empName = emp[k][l].toString();
this.numOfHours = Integer.parseInt(emp[k][l].toString());
this.hourlyRate = Integer.parseInt(emp[k][l].toString());
}
}
}

public String getID()
{
return empID;
}
public String getName()
{
return empName;
}
public int getNumOfHours()
{
return numOfHours;
}
public double getHourlyRate()
{
return hourlyRate;
}

public double gettoTotalPay()
{
return numOfHours * hourlyRate;
}

}


class Employer{
public static void main(String [] args)
{
String empId;
String empName;
String numOfHrs;
String hourlyRate;
Payroll emp1 = new Payroll();
Object [][]emp = new Object[1][1];


Scanner sc = new Scanner(System.in);

for(int i=0;i<emp.length;i++)
{ // start for loop
for(int j=0;j<emp[0].length;j++)
{
emp[i][j]= JOptionPane.showInputDialog(null,"Enter Employer ID");
if ( (emp[i][j].equals("")))
{
JOptionPane.showMessageDialog( null,"Employee ID Not Correct");
emp[i][j]= JOptionPane.showInputDialog(null,"Enter Employer ID");
}
emp[i][j] = JOptionPane.showInputDialog(null,"Enter Employer Name");
if(emp[i][j] == null )
{
JOptionPane.showMessageDialog(null,"Employee Name Cannot Be Empty");
emp[i][j] = JOptionPane.showInputDialog(null,"Enter Employer Name");
}
emp[i][j] = JOptionPane.showInputDialog(null,"Enter Number of Hours Worked on");
if(emp[i][j] == null )
{
JOptionPane.showMessageDialog(null,"Number of Hour Cannot be empty or more than 8");
emp[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number of Hours Worked on"));
}
emp[i][j] = JOptionPane.showInputDialog(null,"Enter Hourly Rate ");
if(emp[i][j] == null)
{
JOptionPane.showMessageDialog(null,"Number of Hourly Rate Cannot be empty or Minus Value");
hourlyRate = JOptionPane.showInputDialog(null,"Enter Hourly Rate ");

}
emp1 = new Payroll(emp,6 ); // this is the 2D array pass to Payroll Class
}//end inner for loop
}//end for loop

System.out.println("Employee Id:" +emp1.getID() );
System.out.println("Employee Name:" +emp1.getName() );
System.out.println("Employee Hours:" +emp1.getNumOfHours());
System.out.println("Employee Total Payment:" +emp1.gettoTotalPay() );

}
}

如果我插入

employe id:1
emaployee name : asd
employee num of hrs:1
employee hourly rate :100

但回答得到

Employee Id:1
Employee Name:1
Employee Hours:1
Employee Total Payment:1.0

请多多指教

最佳答案

只需删除内部 for 循环并添加

++j;

在 Employer 类中有 3 次。

现在它工作正常。你在评论中提到的你得到 nullPointerException 的原因是因为你不去下一列,你停留在第 0 列,为此你必须递增 j

    import java.util.Scanner; 
import javax.swing.*;
class Employer{
public static void main(String [] args)
{
String empId;
String empName;
String numOfHrs;
String hourlyRate;
Payroll emp1 = new Payroll();
Object [][]emp = new Object[3][4];
Scanner sc = new Scanner(System.in);

for(int i=0;i<emp.length;i++)
{ // start for loop
int j=0;
emp[i][j]= JOptionPane.showInputDialog(null,"Enter Employer ID");
if ( (emp[i][j].equals("")))
{
JOptionPane.showMessageDialog( null,"Employee ID Not Correct");
emp[i][j]= JOptionPane.showInputDialog(null,"Enter Employer ID");
}
++j;
emp[i][j] = JOptionPane.showInputDialog(null,"Enter Employer Name");
if(emp[i][j] == null )
{
JOptionPane.showMessageDialog(null,"Employee Name Cannot Be Empty");
emp[i][j] = JOptionPane.showInputDialog(null,"Enter Employer Name");
}
++j;
emp[i][j] = JOptionPane.showInputDialog(null,"Enter Number of Hours Worked on");
if(emp[i][j] == null )
{
JOptionPane.showMessageDialog(null,"Number of Hour Cannot be empty or more than 8");
emp[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number of Hours Worked on"));
}
++j;
emp[i][j] = JOptionPane.showInputDialog(null,"Enter Hourly Rate ");
if(emp[i][j] == null)
{
JOptionPane.showMessageDialog(null,"Number of Hourly Rate Cannot be empty or Minus Value");
hourlyRate = JOptionPane.showInputDialog(null,"Enter Hourly Rate ");

}
emp1 = new Payroll(emp,i ); // this is the 2D array pass to Payroll Class
//end inner for loop
}//end for loop
for(int i=0;i<emp.length;i++)
{
System.out.println("Employee Id:" +emp1.getID() );
System.out.println("Employee Name:" +emp1.getName() );
System.out.println("Employee Hours:" +emp1.getNumOfHours());
System.out.println("Employee Total Payment:" +emp1.gettoTotalPay() );
}

}
}
class Payroll
{
private String empID;
private String empName;
private int numOfHours;
private double hourlyRate;
private double totalPay;

public Payroll(){}

public Payroll(Object[][]emp, int scale)
{

for(int k=0;k<=scale;k++)
{
int l=0;
this.empID = emp[k][l++].toString();
this.empName = emp[k][l++].toString();
this.numOfHours = Integer.parseInt(emp[k][l++].toString());
this.hourlyRate = Float.parseFloat(emp[k][l++].toString());
}
}


public String getID()
{
return empID;
}
public String getName()
{
return empName;
}
public int getNumOfHours()
{
return numOfHours;
}
public double getHourlyRate()
{
return hourlyRate;
}

public double gettoTotalPay()
{
return numOfHours * hourlyRate;
}

}

关于java - 将二维数组传递给构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31451735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com