2014-02-16 125 views
1

我試圖根據功能和輸出基於函數的第一個和第二個最好的PC分值各種「電腦規格」的值進行排序:在數組中查找第一個和第二個?

score[i] = 2*pcram[i] + 3*pccpu[i] + pchdd[i];

的排序是在這裏:

//place sorted data into seperate arrays to make calculations more logical to look at 
    for (i = 0; i < numpc; i++){ 

     pcram[i] = Integer.parseInt(pcname[i][1]); 
     pccpu[i] = Integer.parseInt(pcname[i][2]); 
     pchdd[i] = Integer.parseInt(pcname[i][3]); 

    } 

    //solve the score and find first and second place 
    for (i = 0; i < numpc; i++){ 
     score[i] = 2*pcram[i] + 3*pccpu[i] + pchdd[i]; 
    } 

    for (i = 0; i < numpc - 1; i++){ 
     if (i == 0 && score[i + 1] > score[i]){ 
      first = i + 1; 
      second = i; 
     } 
     if(i == 0 && score[i + 1] > score[i]){ 
      first = i; 
      second = i+1; 
     } 
     if (score[i + 1] > score[i]){ 
      second = first; 
      first = i+1; 
     } 
     if (score[i] > score[i+1]){ 
      second = first; 
      first = i; 
     } 
    } 
    System.out.println(pcname[first][0] + " " + score[first]); 
    System.out.println(pcname[second][0] + " " + score[second] + " " + score[0]); 

會導致一個錯誤的樣品輸入是:

(輸入如下:個人電腦的數目,PC,RAM,CPU名稱,HDD)

4 
Apple 16 3 500 
Dell 16 2 500 
HP 8 2 500 
Custom 1000 1000 1000 

很顯然,該方案輸出自定義爲第一,但後來說,戴爾是第二。我試圖覆蓋所有情況,但無濟於事。提前致謝。

編輯:根據要求,完整的程序。 (這實現了建議的排序方法,原文在上面貼出)

public static void main(String[] args) { 

    Scanner nameinput = new Scanner(System.in); 
    Scanner datainput = new Scanner(System.in); 

    int numpc = datainput.nextInt(); 

    String[][] pcname = new String[numpc][5]; //hold sorted data 

    String[] pcdata = new String[numpc]; //hold unsorted data 

    int i = 0; 
    int first = 0; 
    int second = 0; 

    int[] score = new int[numpc]; 
    int[] pcram = new int[numpc]; 
    int[] pccpu = new int[numpc]; 
    int[] pchdd = new int[numpc]; 

    //begin program 
    for (i = 0; i < numpc; i++){ 

     pcdata[i] = nameinput.nextLine(); //get unsorted data 

    } 

    for (i = 0; i < numpc; i++){ 

     pcname[i] = pcdata[i].split(" "); //sort data 

    } 

    //place sorted data into seperate arrays to make calculations more logical to look at 
    for (i = 0; i < numpc; i++){ 

     pcram[i] = Integer.parseInt(pcname[i][1]); 
     pccpu[i] = Integer.parseInt(pcname[i][2]); 
     pchdd[i] = Integer.parseInt(pcname[i][3]); 

    } 

    //solve the score and find first and second place 
    for (i = 0; i < numpc; i++){ 
     score[i] = 2*pcram[i] + 3*pccpu[i] + pchdd[i]; 
    } 

    for(i = 0; i<score.length-1; i++){  //first find and store the highest values 
     if(score[i]> score[i+1]){ 
      if(score[i]>first){ 
       first = score[i]; 
      } 
      if(score[i+1]>second){ 
       second = score[i+1]; 
      } 
     } else { 
      if(score[i+1]>first){ 
       first = score[i+1]; 
      } 
      if(score[i]>second){ 
       second = score[i]; 
      } 
     } 
    } 
    for(i= 0; i<score.length; i++){  //now get the index of that value 
     if(first == score[i]){ 
      first = i; 
      break; 
     } 
    } 
    for(i= 0; i<score.length; i++){  //index for second 
     if(second == score[i]){ 
      second = i; 
      break; 
     } 
    } 
    System.out.println(pcname[first][0] + " " + score[first]); 
    System.out.println(pcname[second][0] + " " + score[second] + " " + score[0]); 
    nameinput.close(); 
    datainput.close(); 
} 
+0

爲什麼不只是爲該類創建自定義類和「Comparator」? – fge

+0

我被挑戰要在單一課堂上做到這一點:我只是不明白爲什麼比較時程序忽略了'score [0]'。 –

+0

那麼,你知道,封閉的類存在...最終的結果將仍然是一個類... – fge

回答

2

看起來你只是簡單地混合了比較標誌。

for (i = 0; i < numpc - 1; i++){ 
    if (i == 0 && score[i + 1] > score[i]){ 
     first = i + 1; 
     second = i; 
    } 
    if(i == 0 && score[i + 1] < score[i]){ //<--- you had > here 
     first = i; 
     second = i+1; 
    } 
    if (score[i + 1] > score[i]){ 
     second = first; 
     first = i+1; 
    } 
    if (score[i] > score[i+1]){ 
     second = first; 
     first = i; 
    } 
} 

*編輯* 我認爲問題是,你不比較,如果我的價值是在第一或第二實際上大於或小於該值,你應該試試這個:

int first = 0, second = 0; 
    for(i = 0; i<score.length-1; i++){  //first find and store the highest values 
     if(score[i]> score[i+1]){ 
      if(score[i]>first){ 
       second = first; // *NEW* the former first place is now the second ! 
       first = score[i]; //first = 541 //NAN // NAN 
      } 
      if(score[i+1]>second){ //second = 538 //NAN // NAN 
       second = score[i+1]; 
      } 
     } else { 
      if(score[i+1]>first){ 
       second = first; 
       first = score[i+1];//NAN // 522< 541 // first = 6000 
      } 
      if(score[i]>second){ 
       second = score[i];//NAN // NAN // NAN 
      } 
     } 
    } 

注意:奇怪的註釋是跟隨循環中發生的事情。將它們解釋爲colums(其中每列=循環的一個循環)

+0

哈哈似乎是這樣(我感覺超級銳利),但我測試,但它仍然無法正常工作。與我在我的問題中概述的相同的輸入,它輸出自定義爲第一,戴爾爲第二(蘋果應爲第二)。 –

+0

是的,我認爲問題是,你實際上並沒有比較已經在第一和第二的值。見編輯回答 – chrizz42

+0

我不明白爲什麼編輯的代碼不會工作,但程序繼續吐出錯誤的結果。如果你想看到整個代碼,我會很樂意與你分享。感謝您的幫助 –

相關問題