需要将佣金率从一个阵列提取到输出中

时间:2015-10-21 17:42:44

标签: java arrays class

我在初学者Java课程中遇到了麻烦。我需要让我的程序将佣金率从一个数组拉到输出中。其他一切似乎都在起作用,但它不会计算佣金,我不确定我错过了什么。任何帮助都会很精彩!

    public class Annualwages extends salesperson
     {

       /** @param args the command line arguments     */

        public static void main(String[] args) 
        { //begins the main method

         salesperson sp = new salesperson();
         sp.saleInformation();


            System.out.println("You entered the following:");

            System.out.println();
              for (int index = 0; index < sp.salesP.length; index++) 
             {
                System.out.println("Sales Person " + (index + 1));
                System.out.println("   Name: "
                        + sp.salesP[index]);
                System.out.println("   Total Annual Sales: "
                        + sp.sales[index]);
                System.out.println("   Base Salary is: " + sp.SALARY);
                System.out.println("   Annual Commission is: "
                        + sp.comm[index]);
                System.out.println("   Total Annual Compensation is: "
                        + sp.totalWages );
                System.out.println();
            }



            } // ends main method

            } //ends annual wages class
    package annualwages;

    import java.util.Scanner;


    class salesperson { //begins salesperson class
         protected String name;
         protected String[] salesP; // To reference an array of sales people
         protected double[] sales; // references an array for sales achieved
         protected double[] comm; // references an array for commission rate
         protected int numSales; // Number of sales people & sales achieved
         protected final double COMMISSION=0.15; //Sets a fixed variable for      commission earned
         protected final double SALARY=50000; //Sets a fixed variable for Salary earned
         protected final double SALESTARGET=120000; //Sets a fixed variable for the sales target
         protected double totalSales, totalWages, actualCommission, accelFactor=1.25;

         public void saleInformation()
        { //begins saleInformation method
         // Create a Scanner object to read input.
        Scanner keyboard = new Scanner(System.in);

            //Get number of sales people
            System.out.print("How many sales people do you want to compare? ");
            numSales = keyboard.nextInt();

            // Creates an array to hold number of sales people.
            salesP = new String [numSales];

            //Gets the names of the sales people.
            for (int index = 0; index < salesP.length; index++)
            {
                System.out.print("Enter name of sales person "
                          + (index + 1) + ": ");
                salesP[index] = keyboard.next();
                        }

           //creates an array to hold amoount in sales achieved
            sales = new double[numSales];

            // gets the amount in sales that each sales person has achieved  annually
            for (int index = 0; index < sales.length; index++)
            {
                System.out.print("Enter amount in sales achieved per person annually "
                            + (index + 1) + ": ");
                sales[index] = keyboard.nextDouble();
            }

            // creates an array to hold the commission rate
            comm = new double [numSales];

         //Sales incentive begins at a minimum at $96,000 in sales.
         //if less than, then no commission is earned
            for (int index = 0; index < comm.length; index ++)       
            {
                        if (sales.length <= 96000)
                { 
                actualCommission=0;
                }

                    // Sales incentive with $96,000 or more earns 15% commission
                    else if ((sales.length > 96000) && (sales.length < SALESTARGET ))
                        {
                        actualCommission=COMMISSION;
                        }

                    //Sales incentive increases if the sales person earns more than $120,000
                    //in sales with the acceleration factor of 1.25
                    else
                        {
                        actualCommission=COMMISSION*accelFactor;
                        }
            }
                     //Calculates total sales by multiplying sales and commission
                     totalSales = sales.length* comm.length;

                    //Calculates total wages by adding salary to total sales
                     totalWages = SALARY + totalSales;


        } //ends saleInformation method

    } //ends salesperson class

1 个答案:

答案 0 :(得分:0)

你有一些错误。

  • 计算佣金时,您没有为comm[index]分配任何值。不要使用actualCommission变量(您实际上从未显示过),只需直接指定给comm[index]
  • 变量sales.length不太可能达到96000或更高,因为它是您投入的销售数量。因此,第一个if条件将每次返回true时间,你最终将佣金设置为零。也许你的意思是写if (sales[index] <= 96000)。对于涉及sales.length的其他比较也是如此。