我很困惑如何訪問數組temp,以便將當前數組元素temp [i]與最大銷售額0進行比較,以確定哪個更大,每次我嘗試時我都不能在第8步和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]);
}
}
你什麼錯誤,是運行時間或建立時間,和你是什麼願意改變? – nicomp
你需要更具體。 「我無法訪問temp」是非常含糊的,當我們試圖回答你的問題時並不能幫助我們。 – tnw
你正在談論哪個臨時陣列?我認爲你這樣做的方式看起來很好。你能更清楚嗎? –