0

我需要比较至少三个人的年总销售额。我需要我的应用程序来计算每个人必须达到的额外金额才能匹配或超过最高收入者。如果场景中只有两个人,我已经弄清楚了大部分并且知道该怎么做,但是在等式中获得第三名会让我陷入困境!任何帮助表示赞赏,并提前感谢!到目前为止,这是我所拥有的,但显然最后计算不会正确。

package Commission3;
import java.util.Scanner;
public class MainClass {

    public static void main(String[] args) {
        // Create a new object AnnualCompensation
        Commission3 salesPerson[] = new Commission3[2];
        // creat two object
        salesPerson[0] = new Commission3();
        salesPerson[1] = new Commission3();
        salesPerson[2] = new Commission3();
        //new scanner input
        Scanner keyboard = new Scanner(System.in);

        //get salesperson1 name
        System.out.println("What is your first salesperson's name?");
        salesPerson[0].name = keyboard.nextLine();

        //get salesperson1 sales total
        System.out.println("Enter annual sales of first salesperson: ");
        double val = keyboard.nextDouble();
        salesPerson[0].setAnnualSales(val);

        //get salesperson2 name
        System.out.println("What is your second salesperson's name?");
        salesPerson[1].name = keyboard.next();


        //get salesperson2 sales total
        System.out.println("Enter annual sales of second salesperson: ");
        val = keyboard.nextDouble();
        salesPerson[1].setAnnualSales(val);

        //get salesperson3 name
        System.out.println("What is your third salesperson's name?");
        salesPerson[2].name = keyboard.next();


        //get salesperson3 sales total
        System.out.println("Enter annual sales of third salesperson: ");
        val = keyboard.nextDouble();
        salesPerson[2].setAnnualSales(val);


        double total1, total2, total3;
        total1 = salesPerson[0].getTotalSales();
        System.out.println("Total sales of " + salesPerson[0].name +" is: $" + total1);
        total2 = salesPerson[1].getTotalSales();
        System.out.println("Total sales of " + salesPerson[1].name +" is: $" + total2);
        total3 = salesPerson[2].getTotalSales();
        System.out.println("Total sales of " + salesPerson[2].name +" is: $" + total3);
        if (total1 > total2) {
            System.out.print("Salesperson " + salesPerson[2].name + "'s additional amount 
        of sales that he must " + " achieve to match or exceed the higher of the 
        salesperson " + salesPerson[0].name);  
            System.out.println(" $" + (total1 - total2));
        } else if (total2 > total1) {
            System.out.print("Salesperson " + salesPerson[0].name + "'s additional amount
        of sales that he must " + " achieve to match or exceed the higher of the 
        salesperson " +  salesPerson[1].name);

            System.out.println(" $" + (total2 - total1));
        } else {
            System.out.println("Both have same compensation $" + total1);
        }
    }
}
4

1 回答 1

5

当您从用户那里获取输入时,请跟踪迄今为止的最高销售额以及销售额最高的销售人员的姓名。

然后,您可以循环遍历所有三个,而不是检查 total1 和 total2,并将它们与最大值进行比较。如果当前总数小于最大值,则计算差值。否则,当前总和等于最大值,您无需进行计算。

我会留下实际的代码让你弄清楚。

于 2013-06-27T19:13:53.607 回答