2014-02-16 121 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

我不明白为什么编辑的代码不会工作,但程序继续吐出错误的结果。如果你想看到整个代码,我会很乐意与你分享。感谢您的帮助 –

相关问题