试图让销售额最高的部门

时间:2015-04-01 22:57:27

标签: java arrays multidimensional-array

我正在编写一个程序,显示公司中6个不同部门的季度销售额。

我的大部分都在工作,但是当我尝试在每个季度展示销售额最高的部门时,它不起作用。

编辑:我正确地读取了所有输出,但我现在的问题是检查输入的数字是否是> = 0,我尝试过if语句和while循环并执行while循环,但我可以&# 39;弄清楚。

编辑2:我已更新代码以匹配我当前的代码。

 import java.util.Scanner;

public class SalesByQuarter
{
    public static void main(String[] args)
    {
        int divs = 6; 
        int qtrs = 4; 
        double totalSales = 0.0;  
        double errorCheck;

    double[][] sales = new double[divs][qtrs];


    Scanner keyboard = new Scanner(System.in);


    System.out.println("This program will calculate the " +
                       "total sales of");
    System.out.println("all the company's divisions. " +
                       "Enter the following sales data:");

    System.out.println();



        for (int div = 0; div < divs; div++)
        {
            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                System.out.printf("Division %d, Quarter %d: $",
                                (div + 1), (qtr + 1));
                errorCheck = keyboard.nextDouble();
                sales[div][qtr] = errorCheck;
            }
            System.out.println();   
        }


        if (errorCheck < 0)
        {
            System.out.println("Positive numbers only please!");
            for (int div = 0; div < divs; div++)
            {
                for (int qtr = 0; qtr < qtrs; qtr++)
                {
                    System.out.printf("Division %d, Quarter %d: $",
                                    (div + 1), (qtr + 1));
                    errorCheck = keyboard.nextDouble();
                    sales[div][qtr] = errorCheck;
                }
                System.out.println();   
            }
        }

        else
        {
            for (int div = 0; div < divs; div++)
            {
                for (int qtr = 0; qtr < qtrs; qtr++)
                {
                    totalSales += sales[div][qtr];
                }
            }


            double[] divsales = new double[divs];


            for (int div = 0; div < divs; div++)
            {
                for (int qtr = 0; qtr < qtrs; qtr++)
                {
                    divsales[div] += sales[div][qtr];
                }
                System.out.printf("Division %d sales: $%,.2f\n", (div + 1), divsales[div]);
            }


            System.out.println();


            for (int div = 1; div < divs; div++)
            {
                if ((divsales[div] - divsales[div - 1]) > 0)
                {
                    System.out.printf("Division %d increase over its previous quarter: $%,.2f\n", (div + 1), divsales[div] - divsales[(div - 1)]);
                }
                else if ((divsales[div] - divsales[div - 1]) < 0)
                {
                    System.out.printf("Division %d decrease from its previous quarter: $%,.2f\n", (div + 1), divsales[div] - divsales[(div - 1)]);
                }
                else
                {
                    System.out.printf("Division %d had no increase or decrease from its previous quarter.\n", (div + 1));
                }
            }


            System.out.println();


            double[] qtrsales = new double[qtrs];


            for (int div = 0; div < divs; div++)
            {
                for (int qtr = 0; qtr < qtrs; qtr++)
                {
                    qtrsales[qtr] += sales[div][qtr];
                }
            }




            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                System.out.printf("Total sales for quarter: %d: $%,.2f\n", (qtr + 1), qtrsales[qtr]);
            }


            System.out.println();


            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                System.out.printf(" Average sale for quarter: %d: $%,.2f\n", (qtr + 1), (qtrsales[qtr] / divs));
            }


            System.out.println();


            int highestDiv = 0;
            int[] highestDivi = new int[qtrs];
            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                highestDiv = 0;
                for (int div = 0; div < (divs - 1); div++)
                {
                    if (sales[highestDiv][qtr] < sales[(div + 1)][qtr])
                    {
                        highestDiv = (div + 1);
                    }
                }
                highestDivi[qtr] = highestDiv;
            }

            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                System.out.printf("Division %d had the highest sales in quarter %d\n", qtr + 1, highestDivi[qtr] + 1);
            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

看起来你有一些语义错误。我改变了你发现每个季度销售额最高的部门。

int highestDivi[] = new int[qtrs];

double highestDiv = 0;
for (int qtr = 0; qtr < qtrs; qtr++)
{
    highestDiv = 0;
    for (int div = 0; div < divs; div++)
    {
        if (highestDiv < sales[div][qtr])
        {
            highestDivi[qtr] = div;
            highestDiv=sales[div][qrt];
        }
    }
}

关于编辑2: 我更改了用户输入销售的部分,您应该删除代码中的错误检查if语句。

for (int div = 0; div < divs; div++)
{
    for (int qtr = 0; qtr < qtrs; qtr++)
    {
        System.out.printf("Division %d, Quarter %d: $",
                        (div + 1), (qtr + 1));
        errorCheck = keyboard.nextDouble();
        while(errorCheck<0) {
            System.out.println("Positive numbers only please!");
            System.out.printf("Division %d, Quarter %d: $",
                        (div + 1), (qtr + 1));
            errorCheck = keyboard.nextDouble();
        }
        sales[div][qtr] = errorCheck;
    }
    System.out.println();
}

所有内容应如下所示

public class SalesByQuarter
{
    public static void main(String[] args)
    {
        int divs = 6; 
        int qtrs = 4; 
        double totalSales = 0.0;  
        double errorCheck;

    double[][] sales = new double[divs][qtrs];


    Scanner keyboard = new Scanner(System.in);


    System.out.println("This program will calculate the " +
                       "total sales of");
    System.out.println("all the company's divisions. " +
                       "Enter the following sales data:");

    System.out.println();


        for (int div = 0; div < divs; div++)
        {
            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                System.out.printf("Division %d, Quarter %d: $",
                                (div + 1), (qtr + 1));
                errorCheck = keyboard.nextDouble();
                while(errorCheck<0) {
                    System.out.println("Positive numbers only please!");
                    System.out.printf("Division %d, Quarter %d: $",
                                (div + 1), (qtr + 1));
                    errorCheck = keyboard.nextDouble();
                }
                sales[div][qtr] = errorCheck;
            }
            System.out.println();
        }





        for (int div = 0; div < divs; div++)
        {
            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                totalSales += sales[div][qtr];
            }
        }


        double[] divsales = new double[divs];


        for (int div = 0; div < divs; div++)
        {
            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                divsales[div] += sales[div][qtr];
            }
            System.out.printf("Division %d sales: $%,.2f\n", (div + 1), divsales[div]);
        }


        System.out.println();


        for (int div = 1; div < divs; div++)
        {
            if ((divsales[div] - divsales[div - 1]) > 0)
            {
                System.out.printf("Division %d increase over its previous quarter: $%,.2f\n", (div + 1), divsales[div] - divsales[(div - 1)]);
            }
            else if ((divsales[div] - divsales[div - 1]) < 0)
            {
                System.out.printf("Division %d decrease from its previous quarter: $%,.2f\n", (div + 1), divsales[div] - divsales[(div - 1)]);
            }
            else
            {
                System.out.printf("Division %d had no increase or decrease from its previous quarter.\n", (div + 1));
            }
        }


        System.out.println();


        double[] qtrsales = new double[qtrs];


        for (int div = 0; div < divs; div++)
        {
            for (int qtr = 0; qtr < qtrs; qtr++)
            {
                qtrsales[qtr] += sales[div][qtr];
            }
        }




        for (int qtr = 0; qtr < qtrs; qtr++)
        {
            System.out.printf("Total sales for quarter: %d: $%,.2f\n", (qtr + 1), qtrsales[qtr]);
        }


        System.out.println();


        for (int qtr = 0; qtr < qtrs; qtr++)
        {
            System.out.printf(" Average sale for quarter: %d: $%,.2f\n", (qtr + 1), (qtrsales[qtr] / divs));
        }


        System.out.println();


        int highestDiv = 0;
        int[] highestDivi = new int[qtrs];
        for (int qtr = 0; qtr < qtrs; qtr++)
        {
            highestDiv = 0;
            for (int div = 0; div < (divs - 1); div++)
            {
                if (sales[highestDiv][qtr] < sales[(div + 1)][qtr])
                {
                    highestDiv = (div + 1);
                }
            }
            highestDivi[qtr] = highestDiv;
        }

        for (int qtr = 0; qtr < qtrs; qtr++)
        {
            System.out.printf("Division %d had the highest sales in quarter %d\n", qtr + 1, highestDivi[qtr] + 1);
        }

    }

}

干杯!