如何在字符串数组旁边按降序排列数组

时间:2014-11-11 15:54:42

标签: java arrays sorting

这是我的代码......我需要根据他们的投票对所有参赛者进行排名......你可以看到有名单和投票数的单独数组(统计数字)......我被告知泡泡sort会工作,但我不知道如何因为冒泡排序取代内部所以如果tally [2]大于tally [1],它将成为新的计数[1]同时名称保持与名称[1]不相同更改名称[2] ...我希望您使用基本代码,因为我是编程的总菜鸟,并且最近刚刚开始...还有,关于我如何能更好/更短地编写代码的任何建议都值得赞赏< / p>

import java.util.*;
public class FroshNight {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    String dec = "0";
    System.out.println("How many blocks?");
    int contno = sc.nextInt();
    int winner = 0;
    String winname = "a";
    String[] code = new String[contno];
    int total = 0;
    int counter = 0;
    String[] contname = new String[contno];
    int[] tally = new int[contno];
    while (counter<contno) {
        System.out.print("Name of Pair:");
        contname[counter]=sc.next();
        counter++;
    }
    counter=0;
    while (counter<contno) {
        System.out.println("Choose a vote code for "+ contname[counter]+"(must not be x)");
        code[counter] = sc.next();


        counter++;
    }
    boolean stop = false;

    counter=0;
    while(stop == false){
        System.out.println("Input Vote (x to exit): ");
        dec = sc.next();
        if(dec.equals("x")) {
            stop = true;

        }
        counter = 0;
        while(counter<contno){  

            if(dec.equals(code[counter])){
                tally[counter] +=1;
                counter = contno;

            }
            counter++;
        }

    }
    //>> aft. else
    counter = 0;
    while(counter<contno){
        total += tally[counter];
        counter++;
        ;
    }
    counter=0;
    while(counter<contno){
        if(tally[counter]>winner){
            winner = tally[counter];
            winname = contname[counter];
        }
        counter++;
    }
    System.out.print("The Winner is: "+ winname +" - "+winner );
    System.out.println("("+((double)winner/total)*100+"%)");

    counter = 0;
    while (counter<contno) {
        System.out.print(contname[counter]+" - Score:");
        System.out.print(tally[counter]);
        System.out.println("("+((double)tally[counter]/total)*100+"%)");

        counter++;

    }

    //>>> end while

    // add rankings here^^
}

    }

0 个答案:

没有答案