【问题标题】:Accessing array in private class在私有类中访问数组
【发布时间】:2016-07-25 17:57:29
【问题描述】:

我对如何访问数组 temp 感到困惑,以便将当前数组元素 temp[i] 与为 0 的最大销售额进行比较以确定哪个更大,每次我尝试在步骤 8 中都无法访问 temp和 STEP 7,我不想改变类的可见性

public class Sales {
    public static void main(String[] args) {
        int[] sales;

        sales = getSales();
        printSales(sales);
        printSummary(sales);
    }

    private static int[] getSales() {
        Scanner input = new Scanner(System.in);
        int[] temp;

        System.out.print("Enter the number of salespeople: ");
        temp = new int[input.nextInt()]; // Step 1

        for (int i = 0; i < temp.length; i++) // Step 2
        {
            System.out.print("Enter sales for salesperson " + (i + 1) + ": ");
            temp[i] = input.nextInt(); // Step 3
        }
        return temp; // Step 4
    }

    private static void printSales(int[] s) {
        System.out.println();
        System.out.println("Salesperson   Sales");
        System.out.println("-----------   -----");
        for (int i = 0; i < 5; i++) // Step 5
        {
            System.out.printf("%6d%12d\n", i + 1, s[i]); // Step 6
        }
    }

    private static void printSummary(int[] s) {
        int sum = 0;
        int max_sale = 0; // Salesperson with the most sales
        int min_sale = 0; // Salesperson with the least sales

        for (int i = 0; i < ________; i++) // Step 7
        {
            ____________ // Step 8

        }
        System.out.println();
        System.out.println("Total sales:  " + sum);
        System.out.println("Average sales: " + (double) sum / s.length);
        System.out.println("Salesperson " + (max_sale + 1) + " had the maximum sale with " + s[max_sale]);
        System.out.println("Salesperson " + (min_sale + 1) + " had the minimum sale with " + s[min_sale]);
    }
}

【问题讨论】:

  • 你得到了什么错误,是运行时还是构建时,你愿意改变什么?
  • 您需要更具体。 i cannot access temp 非常含糊,在我们试图回答您的问题时对我们没有帮助。
  • 你说的是哪个临时数组?我认为你这样做的方式看起来不错。能说清楚一点吗?
  • 我还可以建议更好的缩进,它会让你的代码更容易理解。
  • 我已经更新了资料

标签: java arrays private


【解决方案1】:

感谢@andrew,这在第 7/8 步有效

int max = 0; 
int min = Integer.MAX_VALUE; 

for (int i= 0; i < s.length; i++) { 
sum += s[i]; 
if (s[i] > max) { 
max_sale = i; 
max = s[i]; 
} 
if (s[i] < min) { 
min_sale = i; 
min = s[i]; 
} 
}

【讨论】:

    【解决方案2】:

    获取 Andrew Tobilko 代码并更改它以将索引保存到具有最高销售价值的人。实现这个替换你的循环将起作用。

    for (int i = 0; i < s.length; i++) { // STEP 7
        if (s[i] > s[max_sale]) max_sale = i; // STEP 8
        if (s[i] < s[min_sale]) min_sale = i;
        sum += s[i];
    }
    

    【讨论】:

      【解决方案3】:

      temp 是在main 中创建并传递给您的printSummary(int[] s) 方法的局部变量,因此您可以使用s 访问它。

      for (int i = 0; i < s.length; i++) { // STEP 7
          if (s[i] > max_sale) max_sale = s[i]; // STEP 8
          if (s[i] < min_sale) min_sale = s[i];
          sum += s[i];
      }
      

      【讨论】:

      • 它会产生正确的结果,但也会在线程“main”中产生异常 java.lang.ArrayIndexOutOfBoundsException: 1535 at Sales.printSummary(Sales.java:61) at Sales.main(Sales.java:11)知道为什么吗??
      • @Brent,这个语句不会抛出任何异常,我保证
      • @Brent,使用max_sale 而不是s[max_sale]
      • 虽然它确实产生了正确的总销售额和平均销售额,但之后它会出现错误,这会出现“线程中的异常”main”java.lang.ArrayIndexOutOfBoundsException:Sales.printSummary(Sales.java)中的 300 :59) 在 Sales.main(Sales.java:11) "
      • @Brent, ArrayIndexOutOfBoundsException 表示您正在尝试获取不存在的元素
      猜你喜欢
      • 2017-12-15
      • 2016-02-20
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多