2016-10-03 56 views
-1

所以即時工作的任務和我差不多完成了,但我找不到解決方案,適合我。所以我有這些陣列,1要求一個名稱,2要求工作時間和小時率。隨着小時費率和工作時間,我然後創建另一個陣列,給出總薪水。查找數組的最大值,並讓程序給我數組的數量

所以我需要打印出最高的薪水和那個擁有它們的人的名字。

因此,我正在尋找一種方法,可以給我最大值所在的插槽號,所以我可以使用System.out.Println(「a [maxslotnr] +」具有最高的薪水,這是「」d [maxslotnr])

下面是代碼

public class MaxLaun { 
    public static void main(String args[]) { 

    StdOut.print("what is the amount of employees "); 
    int amount = StdIn.readInt(); 
    String[] a = new String[number]; 
    int[] b = new int[number]; 
    int[] c = new int[number]; 
    int[] d = new int[number]; 



    for (int i=0; i < amount; i++) { 
     StdOut.print("Name of employee"); 
     String name = StdIn.readString(); 
     a[i] = name; 
     StdOut.print("hourly rate"); 
     int rate = StdIn.readInt(); 
     b[i] = rate; 
     StdOut.print("amount of worktime"); 
     int time = StdIn.readInt(); 
     c[i] = time; 
    } 

    for (int j=0; j < number; j++) 
    d[j]= (b[j] * c[j]); 

    } 
} 
+0

注:你是'number'創建陣列,而不是'amount',它是一個錯字? (在第二個循環中相同)(爲什麼第二個循環?在第一個循環中執行b * c) – AxelH

回答

1

當所以你應該使用英語儘可能多地問。即使在你的代碼中。

我覺得這是你在找什麼:

int maxValue = 0; 
    int pos = 0; 

    for (int j = 0; j < fjoldi; j++) { 
     if (d[j] > maxValue) { 
      maxValue = d[j]; 
      pos = j; 
     } 
    } 
+0

嘿,謝謝你的快速回復!我把你的文字編輯成英文,以便你更好地理解它:P和是的,我認爲你是對的。我會去嘗試一下。 –

+0

您可以刪除maxValue並直接使用索引;) – AxelH

+0

由於@AxelH表示可以將maxValue與 'int pos = 0;對於(int j = 0; j d [pos]){ pos = j; } }' 但是這增加了新編碼器的複雜性。我希望代碼簡單。 – gfelisberto