你能给我个想法如何将2D数组传递给构造函数吗?我已经给数组传递了值,但是iam得到了错误的答案。在这里iam共享了我的代码。

    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;


雇主班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 - 将2D数组传递给构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31451735/

10-10 18:25