2016-10-23 52 views
0

我是新來的Java,並已考慮到要求分配:使用陣列式打印的最低和第二最低值的方法(JAVA)

說明

一羣朋友決定運行波士頓馬拉松賽。他們的名字和時間(分鐘)如下:

Name       Time(minutes) 

Elena       341 
Thomas      273 
Hamilton    278 
Suzie       329 
Phil       445 
Matt       402 
Alex       388 
Emma       275 
John       243 
James       334 
Jane       412 
Emily       393 
Daniel     299 
Neda       343 
Aaron       317  
Kate       265 

找到最快的跑步者。打印名稱和他/她的時間(以分鐘爲單位)。

尋找第二快的跑步者。打印名稱和他/她的時間(以分鐘爲單位)。

說明書

編寫作爲輸入整數的數組,並返回對應於 人具有最低時間索引的方法。在時間數組上運行此方法。打印出與返回索引對應的名稱和時間 。 寫第二種方法找到第二好的跑步者。第二種方法應該使用第一種方法 來確定最佳跑步者,然後遍歷所有值以找到第二好的(第二最低)時間。 這裏是一個程序骨架開始:

class Marathon { 
public static void main (String[] arguments){ 
String[] names ={ 
"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", 
"Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", 
"Aaron", "Kate" 
}; 
int[] times ={ 
341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 
343, 317, 265 
}; 
int i1 = fastestIndex(times); 
int i2 = secondFastestIndex(times); 
// 
// your output based on the index i1 and i2 
// 
} 
// your two Java methods: 
// fastestIndex() and secondFastestIndex() 
} 

我寫的打印第一和第二位的時間和名稱,但現在我開始以爲我沒有正確地按照指令的程序。任何人都可以幫我弄清楚我應該做什麼不同嗎?

這裏是我的業餘代碼:

public class Lab 
{ 
    public static void main(String[] args) 
    { 
     String[] names = { 
      "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", 
      "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate" 
     }; 

     int[] times = { 
      341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265 
     }; 

     int firstplace = times[0]; 
     String firstname = names[0]; 

     int secondplace = times[0]; 
     String secondname = names[0]; 

     for (int counter = 0; counter < times.length; counter++) 
     { 
      if (times[counter] < firstplace) { 
       firstplace = times[counter]; 
       firstname = names[counter]; } 
     } 

     for (int counter = 0; counter < times.length; counter++) 
     { 
      if (times[counter] > firstplace) 
       if (times[counter] < secondplace) { 
        secondplace = times[counter]; 
        secondname = names[counter]; } 
     } 
      System.out.printf("The fastest runner is: %s (%d Minutes)%n", firstname, firstplace); 
      System.out.printf("The second fastest runner is: %s (%d Minutes)%n", secondname, secondplace); 
     } // end main 
    } // end class Lab 

這是它打印:

The fastest runner is: John (243 Minutes) 
The second fastest runner is: Kate (265 Minutes) 

回答

0

好吧所以你想要的完整的答案是這樣的:

你的老師給你如何解決這個問題,您應該遵循特定的僞代碼它。

編寫一個方法,該方法將一個整數數組作爲輸入,並返回對應於具有最低時間的人的索引。

這是它:public static int fastestIndex(int[] times){...} 這是static,因爲它不是一個實例方法(這意味着你不必創建一個對象的實例來稱呼它)

,第二個也有相同的簽名

public static int secondFastestIndex(int[] times) 

和裏面調用第一個方法來幫助找到第二個最快的指數

那麼你同時打印。

下面是完整的代碼

public class Lab 
{ 
    public static void main(String[] args) 
    { 
      String[] names = { 
       "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", 
       "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate" 
      }; 

      int[] times = { 
       341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265 
      }; 



      int win = fastestIndex(times); 
      int second = secondFastestIndex(times); 
      System.out.printf("The fastest runner is: %s (%d Minutes)%n", names[win], times[win]); 
      System.out.printf("The second fastest runner is: %s (%d Minutes)%n", names[second], times[second]); 
    } // end main 



    public static int fastestIndex(int[] times) 
    { 

     int fastestIndex = 0; 

     for (int counter = 0; counter < times.length; counter++) 
     { 

      if (times[counter] < times[fastestIndex]) 
      { 
       fastestIndex = counter; 
      } 
     } 

     return fastestIndex; 

    } 

    public static int secondFastestIndex(int[] times) 
    { 
     int fastestIdx = fastestIndex(times); 
     int secondFastestIdx = 0; 

     for (int counter = 0; counter < times.length; counter++) 
     { 
      //you search for the fastest, but you ignore it if it is 
      // equal to the fastest index (so you are really finding the 2nd) 
      if (times[counter] < times[secondFastestIdx] && counter != fastestIdx) 
      { 
       secondFastestIdx = counter; 
      } 
     } 

     return secondFastestIdx; 
    } 

} // end class Lab 

希望這有助於

+0

謝謝你的迴應,這非常有幫助。我認爲有一個我錯過的概念,所以我一直在閱讀教科書和你的代碼,以更好地理解這一點。現在我的代碼正在編譯,但是當我在命令提示符下運行它時,它會掛起而沒有輸出。 – presence

+0

@presence你是否需要進一步的幫助?我上面發佈的代碼在我的eclipse中運行 – HenriqueMS

-1

搜索是好的。只要把它們作爲一種方法,並從主程序中調用這些方法即可。指定調用方法的指令。

將方法firstplace(int [] times)放在第一個搜索循環中,並執行此方法中的第一個搜索位置。第二名也是如此。

我是編程新手,這是我覺得會使代碼遵循指示。

2

由於這是一項任務,我不能告訴你確切的代碼,但這裏是你可以做的。 將代碼分解成執行特定任務的函數是一個更好的主意,這就是您的初學者代碼所說的。您需要編寫兩個函數fasterIndex和secondFastestIndex。最快的索引是應該返回最快索引的函數,secondFastestIndex應該返回第二個最快的索引,所以只需構造代碼,以便執行任務的代碼位於相應的方法中(可以通過移動當前片段的代碼添加到新的方法中),然後調用secondFastestIndex,然後從main方法中調用它們。 這是我能解釋的最好的。基本的骨架應該是這樣的:

void main() { 
    // do some task; 
    // call fastestIndex; 
    // call secondFastestIndex; 
} 

int fastestIndex() { 
    // code 
    int firstplace = times[0]; 
    String firstname = names[0]; 
    for (int counter = 0; counter < times.length; counter++) 
     { 
      if (times[counter] < firstplace) { 
       firstplace = times[counter]; 
       firstname = names[counter]; } 
     } 
    // return stuff 
} 

//Same for secondFastestIndex 

只是要解決像骨架重構代碼,你會沒事的。 另外請確保從secondFastestIndex調用fasterIndex以避免冗餘,並完全按照說明操作。

0

是的,您很大程度上錯過了問題中的說明。

第二種方法應該使用第一種方法來確定最佳跑步者,然後遍歷所有值以找到第二好的(第二最低)時間。

這顯然意味着,secondFastestIndex()應該首先調用fastestIndex(),然後使用返回值來評估第二個跑得最快的人,我不要在您的代碼看。下面是應該滿足它的那種執行:

int secondFastestIndex(){ 
    //... 
    fastestRunnerIndex = fastestIndex() 
    //Run loop again on the array and ignore the fastestRunnerIndex while finding the smallest number in it. 
    return secondFastestRunnerIndex; 
} 
+0

我很抱歉,我一直在尋找由OP不是在指令的代碼編寫的代碼。我的錯。我會刪除我的其他評論,因爲這是完全錯誤的。 –