简体   繁体   中英

Finding First and Second in an Array of Values?

I am attempting to sort the values of various "pc specs" based on a function and output the first and second best pc scores based on the function:

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

The sorting is here:

    //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]);

A sample input that causes an error is:

(The input is as follows: number of PCs, Name of PC, RAM, CPU, HDD)

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

Obviously, the program outputs Custom as first but then goes to say that Dell is second. I have attempted to cover all scenarios with no avail. Thanks in advance.

Edit : As requested, the full program. (This implements a suggested sorting method, the original is posted above)

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();  
}

Looks like you simply mixed up the comparator signs.

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;
    }
}

* EDIT * I think the problem is that you don't compare if the value of i is actually greater or less than the value in first or second, you should try this one instead:

    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
            }
        }   
    }

Note: The strange comments are to follow what happens in the loop. Interpret them as colums (where each column = one cycle of the loop)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM