让方法正常运行

时间:2014-04-08 12:52:52

标签: java arrays methods

我的问题是下面的方法是接受正确的数组变量。但是该方法需要比较employeeId []数组中的用户id,并使用find元素函数来查看id是否在数组中。但我似乎无法让它运行正确或在我试图制作的格式化表中正确计算/输出数据。

public static int findElement(int [] employeeId, int idNumber)
{
    for (int i = 0;i<employeeId.length; i++) 
    {
        if( employeeId[i] == idNumber )
            return i;
        //System.out.println(employeeId[i]);
    }
    return -1;
}

    public static void computePrintWages(int [] employeeId, int [] dependents, double [] hours, double [] payRate, int idNumber)
    {
        float gross;
        double federal =0.0;
        double state = 0.0;
        double net = 0.0;
        double grossTotal = 0.0;
        double federalTotal = 0.0;
        double stateTotal = 0.0;
        double netTotal = 0.0;
        double overtime = 0.0;

        int inArray;
        inArray=findElement(employeeId,idNumber);
        if(inArray!=-1)
        {
             for(int i = 0;i<employeeId.length;i++)
             {
                 gross = (float)(hours[i]*payRate[i]);
                if (hours[i]>40)
                {
                    gross = (float)(hours[i]*payRate[i]);
                    overtime = hours[i] * (payRate[i]*1.5);
                    gross = (float) (gross + overtime); 
                    if(gross!=0)
                    {

                    federal = (gross-(dependents[i]*38.46))*.2;
                    state = .032*gross; 
                    net = gross - (federal+state);
                    grossTotal =  grossTotal + grossTotal++;
                    federalTotal =  federalTotal +federalTotal++;
                    stateTotal = stateTotal + stateTotal++;
                    netTotal =  netTotal + netTotal++;
                    }

                }

                else if(gross!=0)
                {

                federal = (gross-(dependents[i]*38.46))*.2;
                state = .032*gross; 
                net = (gross - (federal+state));
                grossTotal = grossTotal + grossTotal++;
                federalTotal = federalTotal + federalTotal++;
                stateTotal =  stateTotal + stateTotal++;
                netTotal =   netTotal +netTotal++;

                }

                System.out.printf("%-15d %.01f %15f %10f %10f%n", employeeId[i], gross, federal, state, net);
                System.out.printf("total" +"           %.01f %15f %10f %10f%n", grossTotal, federalTotal, stateTotal, netTotal);    
             }
            }
            //System.out.printf("%-15d %.01f % 15f%n", employeeId[i], gross, federal  , state, net) ;
            //System.out.printf("%-15d %.01f %15f %10f %10f%n", employeeId[i], gross, federal, state, net);
            //System.out.printf("total" +"           %.01f %15f %10f %10f%n", grossTotal, federalTotal, stateTotal, netTotal);



        }

1 个答案:

答案 0 :(得分:2)

摆脱循环。您应该使用从 findElement 方法调用获得的索引。如果不是-1,请使用 inArray 的值。 e.g。

gross = (float)hours[inArray] * payRate[inArray];