简体   繁体   English

如何将数组中的数字按降序排列?

[英]how to shift numbers in an array to be in descending order?

Im writing a program which takes in user inputted digits to store as a high score table, the user decides how many numbers on the table, and then they type the numbers they want to appear on the table, although for some reason my code for putting the numbers on the table doesnt put the numbers in descending order as id like. 我编写了一个程序,该程序将用户输入的数字存储为高分表,用户决定该表上有多少个数字,然后他们键入要显示在表上的数字,尽管出于某种原因我的代码表格上的数字不会像id一样按降序排列。 I think it may be a problem with the for loops in the printHighScores function although i dont see how its wrong, it should keep comparing the numbers to see if theyre all in descending order but i think i may have made a mistake int he for loop operations... 我认为printHighScores函数中的for循环可能是个问题,尽管我看不出它有什么问题,它应该不断比较数字以查看它们是否全部都是降序排列,但是我认为我可能在in for loop中犯了一个错误操作...

My code inputs the size of the array, then it takes the numbers in and stores them accordingly in their array position, although it doesnt do the computation to store them in descending order? 我的代码输入了数组的大小,然后将数字输入并相应地将它们存储在它们的数组位置,尽管它不进行计算以将它们降序存储? If the user types the size of the array as 3, and inputs 100, 150, 433, I want the code to sort that so that it outputs 如果用户将数组的大小键入3,并输入100、150、433,则我希望代码对其进行排序,以便输出

  1. 433 433
  2. 150 150
  3. 100 100

instead its printing out 而是打印出来

  1. 100 100
  2. 150 150
  3. 433 433

How come the for loop computation doesnt work and is there any better way to sort out the elements in the array to make them in descending order? 为什么for循环计算不起作用,还有没有更好的方法来整理数组中的元素以使其降序排列?

this is my code here, thanks for any help: 这是我的代码,感谢您的帮助:

import java.util.Scanner;
/*
 * Write a program to maintain a list of the high scores obtained in a game.  
 * The program should first ask the user how many scores they want to maintain and then repeatedly accept new scores
 * from the user and should add the score to the list of high scores (in the appropriate position) if it is higher than any of the existing high scores.
 *   You must include the following functions:

    -initialiseHighScores () which sets all high scores to zero.

    -printHighScores() which prints the high scores in the format: 
        “The high scores are 345, 300, 234”, for all exisiting high scores in the list (remember that sometimes it won’t be full).

    -higherThan() which takes the high scores and a new score and returns whether the passed score is higher than any of those in the high score list.

    -insertScore() which takes the current high score list  and a new score and updates it by inserting the new score at the appropriate position in the list
 */
public class HighScores {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("How many values would you like to set the High Score list too?:");
        int userInput = input.nextInt();
        int[] zeroSet = {};
        int[] setScores = intialisingHighScores(userInput, zeroSet);
        System.out.println("Now Enter the high scores you wish to display:");
        int[] highScores = printHighScores(setScores, userInput);
        System.out.println("The high scores are:");

        for (int i = 0; i <= (userInput-1); i++){
            System.out.println((i+1) + ". " + highScores[i]);
        }
    }
    public static int[] intialisingHighScores(int userInput, int[] zeroSet){
        zeroSet = new int [userInput];
        for (int index = 0; index <= (userInput-1); index++)
        {
            zeroSet[index] = 0;
        }

        return zeroSet;
    }
    public static int[] printHighScores(int[] setScores, int userInput) {
        Scanner inputNo = new Scanner(System.in);
        int[] setScore = {};
        for (int i = 0; i <= (userInput-1); i++)
        {
            int scores = inputNo.nextInt();
            if(scores<0){
                System.out.println("No player can be that bad, please enter positive high scores only");
                scores = inputNo.nextInt();
                setScores[i] = scores;
            }
            else {
                setScores[i] = scores;
            }
        }
        for (int i = 0; i >= (userInput-1); i++){
            for (int n = 0; n <= (userInput-1); n++){
                if (setScore[i] < setScore[n]){
                    int saveNo = 0;
                    for (int p = (userInput-1); p >= 0; p--){
                        setScore[i] = saveNo;
                        setScore[p] = setScore[p+1];
                    }
                    setScore[userInput-1] = saveNo;
                }
            }
        }
        return setScores;
    }
    public static int[] higherThan(int[] printHighScores, int[] setScores){
        return setScores;
    }
}

Solution for Integer: 整数解决方案:

Arrays.sort(array, Collections.reverseOrder());

Or you can use Arrays.sort(array); 或者您可以使用Arrays.sort(array); and then reverse it. 然后将其反转。

for(int i = 0; i < array.length / 2; i++){
    int temp = array[i];
    array[i] = array[array.length - i - 1];
    array[array.length - i - 1] = temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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