从销售周获得最低销售额

时间:2013-06-21 13:32:10

标签: java

我的任务要求我获得8周的销售额,获得最高销售额和最低销售额。虽然我无法弄清楚如何获得最低的一周和价值观。我是Java的新手

这是我的代码:

   public static void main(String[] args) {

        int totalSales = 0;
        int averageSales = 0;
        int highestSale = 0;
        int highestWeek = 0;
        int lowestSale = 0;
        int lowestWeek = 0;

        for(int i = 0; i < 8; i++) {

           int s = Integer.parseInt(JOptionPane.showInputDialog("Week " + (i+1) + ":"));

           totalSales += s;

           if(s > highestSale) {

                highestSale = s;
                highestWeek = i + 1;

            }
           if(lowestSale < s) {

                lowestSale = s;
                lowestWeek = i + 1;

            }
           }

          averageSales += totalSales / 8;

           String msg = "Total sales: £" + totalSales + "\n";
           msg += "Average sales: £" + averageSales + "\n";
           msg += "Highest week was " + highestWeek + ", sold £" + highestSale + "\n";
           msg += "Lowest week was " + lowestWeek + ", sold £" + lowestSale;

           JOptionPane.showMessageDialog(null, msg);

        }

1 个答案:

答案 0 :(得分:4)

if(lowestSale < s) {
    lowestSale = s;
    lowestWeek = i + 1;
  }

这应该是这样的:

if(s < lowestSale) {
     lowestSale = s;
     lowestWeek = i + 1;
 }