如何确定为什么每周收入在我的计划中没有显示前三次?

时间:2017-04-19 17:11:30

标签: java arrays

每周收入没有显示前3次。它宣称大小为4.每周收入计算4次,但没有显示。只有第四次显示。

    package test;

    import java.util.*;

    public class test {
        public static void main(String[] args) {
            String Name[]= new String[4];
            double hoursWorked[] =new double[4];
            double hourlyPay[] = new double[4];
            double WeeklyEarnings[] =new double[4];


            GetInfo(Name,hoursWorked,hourlyPay);
            WeeklyEarnings[3] = CalculateTotal(hoursWorked,hourlyPay);
            DisplayInfo(Name,hoursWorked,hourlyPay,WeeklyEarnings); 
        }

         public static void GetInfo(String Name[], double hoursWorked[], double hourlyPay[])
         {

             String blank;
             Scanner UserIn = new Scanner(System.in);
             for(int i=0;i< Name.length;i++)
             {


                 System.out.print("Please enter name #"+(i+1) + ":");
                 Name[i]= UserIn.nextLine();


                 do
                 {
                 System.out.print("Please enter the number of hours worked:");
                 hoursWorked[i] = UserIn.nextDouble();
                 if(hoursWorked[i]<0)
                 {
                     System.out.print("Invalid entry !!! Please try again");

                 }
                 }while(hoursWorked[i]<0);

                 System.out.print("Please enter the hourly pay rate: ");
                 hourlyPay[i]= UserIn.nextDouble();

                 blank= UserIn.nextLine();
                 System.out.print("\n");

             } 
         }

         public static double CalculateTotal(double hoursWorked[], double hourlyPay[])
            {
                double[] WeeklyEarnings =new double[4];

                for(int i=0;i<hoursWorked.length;i++)
                {
                WeeklyEarnings[i] = hoursWorked[i] * hourlyPay[i];

                }


                return WeeklyEarnings[3];

            }


     public static void DisplayInfo(String Name[], double hoursWorked[], double hourlyPay[], double weeklyEarnings[])
             {

    System.out.print("\nName\t Hours Worked\t Hourly Pay\t Weekly Earnings");

    for(int i=0;i<weeklyEarnings.length;i++)
    {
        System.out.printf("\n"+ Name[i]+"\t"     +hoursWorked[i] +"\t\t"  +"$"     +hourlyPay[i] +"\t\t" +     "$%.2f",weeklyEarnings[i]);


    }


             }




         } 

2 个答案:

答案 0 :(得分:1)

  public static double CalculateTotal(double hoursWorked[], double hourlyPay[])
        {
            double[] WeeklyEarnings =new double[4];

            for(int i=0;i<hoursWorked.length;i++)
            {
            WeeklyEarnings[i] = hoursWorked[i] * hourlyPay[i];

            }


            return WeeklyEarnings[3];

        }

你在这里做的是只撤回数组的一个元素(第四个)而不是你应该返回所有的数组并操纵它

     public static double[] CalculateTotal(double hoursWorked[], double hourlyPay[])
        {
            double[] WeeklyEarnings =new double[4];

            for(int i=0;i<hoursWorked.length;i++)
            {
            WeeklyEarnings[i] = hoursWorked[i] * hourlyPay[i];

            }


            return WeeklyEarnings;

        }

答案 1 :(得分:0)

return WeeklyEarnings[3]更改为return WeeklyEarnings[]

您要求该方法仅返回第4个索引,而不是返回数组的所有索引。