【问题标题】:I'm basically finish with the class, but I still get an error, every time I call the getWage from the main class it display 0.00?我基本上完成了课程,但我仍然得到一个错误,每次我从主课程调用 getWage 它显示 0.00?
【发布时间】:2016-08-18 01:00:37
【问题描述】:

我真的需要帮助,我一直在尝试自学 java,现在我在数组中,我在 3 天内完成了这一章。我想这就是我仍然犯错误的原因之一。任何如何解决此错误的建议都很棒。此外,任何关于如何改进这门课的建议也会有所帮助,谢谢。

 package chapter7;

    public class PayRoll {

        private int[] employeeid = {5658845,    //Employee identification numbers.
                                    4520125,
                                    7895122,
                                    8777541,
                                    8451277,
                                    1302850,
                                    7580489};

        private int[] hours = new int[7];           //Numbers of hours worked by each employee.
        private double[] payRate = new double[7];   //each employee's hourly pay rate.
        private double[] wages = new double[7];    //each employee's gross pay.

        public void setHours(int[] h){

            for(int index = 0; index<h.length; index++){
                hours[index] = h[index];
            }
        }

        /**
         * The setPayRate method copy 
         * an array's elements and stored
         * it in the payRate array field. 
         * @param p Each employee's hourly
         * pay rate.
         */

        public void setPayRate(double[] p){

            for(int index = 0; index<p.length; index++){
                payRate[index] = p[index];
            }
        }

        /**
         * The setWages method copy 
         * an array's elements and stored
         * it in the wages array field. 
         */

        public void setWages(){

            for(int n = 0; n<wages.length; n++){
                wages[n] = (payRate[n]*hours[n]);
            }
        }

        /**
         * The getID method find the employee's
         * subscript.
         * @param id An employee's id.
         * @return An employee subscript.
         */

        public int getID(int id){

            int emp = 0;

            for(int index = 0; index<employeeid.length; index++)
            {
                if(employeeid[index] == id){
                    emp = index;
                }
            }
                return emp;
        }

        /**
         * The getHours method find
         * the numbers of hours worked by
         * an employee.
         * @return An employee's worked hours.
         */

        public int getHours(){

                return hours[getID(0)];
        }

        /**
         * The getPayRate method find
         * the pay rate of an employee.
         * @return An employee's payRate.
         */

        public double getPayRate(){
            return payRate[getID(0)];
        }

        /**
         * The getSize method 
         * @return The numbers of employees.
         */

        public int getSize(){
            return employeeid.length;
        }

        /**
         * The getWage 
         * @return An employee's gross wage.
         */

        public double getWage(){
            return wages[getID(0)];
        }
    }

这里是主类。

package chapter7;

import java.util.Scanner;

public class PayRollTest {

    public static void main(String[]args){

        int  h = 0;     //Hours
        double  p = 0 ; //Pay rate

        Scanner keyboard = new Scanner(System.in);

        PayRoll pay = new PayRoll();

        int[] hours = new int[pay.getSize()];   
        double[] payRate = new double[pay.getSize()];


        //Store the number of hours of each employee.
        for(int index = 0; index<hours.length; index++){

            System.out.println("Enter the number of hours"
                               +" work employee #" + (index+1)
                               + ":");
            h = keyboard.nextInt();

            //Input validation statement for # of hours.
            while(h<1){
                System.out.println("Invalid input!");
                System.out.println("number of hours worked should be >=1");
                 h = keyboard.nextInt();
            }
            hours[index] =  h;
        }

        //Store the  hourly pay rate  of each employee.
        for(int index = 0; index<hours.length; index++){

            System.out.println("Enter the pay rate for"
                               +" employee #" + (index+1)
                               + ":");
            p = keyboard.nextDouble();

            //Input validation for pay rate.
            while(p<9.25){
                System.out.println("Invalid input!");
                System.out.println("Pay rate should be >=9.25");
                 p = keyboard.nextInt();
            }
            payRate[index] = p;
        }

        //pass the array as an argument to the class pay.
        pay.setHours(hours);
        pay.setPayRate(payRate);

        //Prompt the user for an employee id.
        System.out.println("Enter your employee id");
        pay.getID(keyboard.nextInt());

        System.out.println("Hourly Pay Rate: " + pay.getPayRate() +
                            "\nHours Worked: " + pay.getHours() +
                           "\nGross wage: " + pay.getWage());
    }//End of class.
}//End of main.

【问题讨论】:

  • 调用代码是什么样的,你调用方法的顺序是否正确?
  • edit 你的代码包含一个实际的minimal reproducible example。你的public static void main(String args[]) 方法在哪里?
  • 另外你为什么要使用index 0 来表示public double getWage() 之类的方法?
  • 还将您的 employeeId 更改为字符串值
  • @ScaryWombat 我将索引 0 用于 public double getWage() 之类的方法,因为 getID() 方法中有一个参数,我不知道另一种调用有参数的方法的方法并没有在上面放任何东西,我已经测试了主类,一切正常,除了当我调用 getWage() 时,我得到一个 0.00

标签: java arrays class


【解决方案1】:

您没有调用public void setWages(),因此永远不会填写工资数组。

pay.setHours(hours);
pay.setPayRate(payRate);

// add this
pay.setWages ();

您还应该将 getWage 方法更改为类似

 public double getWage(int id){
        return wages[getID(id)];
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多