简体   繁体   English

访问私有类中的数组

[英]Accessing array in private class

I am confused on how to access the array temp in order to compare the current array element temp[i] to max sales which is 0 in order to determine which is bigger,each time i try i cannot access temp IN STEP 8 and STEP 7, i do not want to change the visibility of the classes 我对如何访问数组温度感到困惑,以便将当前数组元素temp [i]与最大销售量0进行比较,以确定哪个更大,每次尝试在STEP 8和STEP 7中我都无法访问temp ,我不想更改班级的知名度

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]);
    }
}

temp is a local variable that had been created in the main and passed to your printSummary(int[] s) method, so there you can access it by using s . 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];
}

Took Andrew Tobilko code and changed it to save the index to the person that has highest sale value. 拿了安德鲁·托比尔科(Andrew Tobilko)代码并将其更改为将索引保存到具有最高销售价值的人。 Implementing this replacing your loop will work. 实现此替换循环将起作用。

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];
}

This works at step 7/8 thanks to @andrew 感谢@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]; 
} 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM