【问题标题】:how to shift numbers in an array to be in descending order?如何将数组中的数字按降序排列?
【发布时间】:2014-12-07 00:10:53
【问题描述】:

我正在编写一个程序,它将用户输入的数字存储为高分表,用户决定表上有多少个数字,然后他们输入他们想要出现在表上的数字,尽管出于某种原因我的将数字放在桌子上的代码不会像 id 那样将数字按降序排列。我认为这可能是 printHighScores 函数中的 for 循环的问题,尽管我看不出它有什么问题,它应该继续比较数字以查看它们是否都按降序排列,但我认为我可能在他的 for 循环中犯了一个错误操作...

我的代码输入数组的大小,然后将数字输入并相应地将它们存储在它们的数组位置中,尽管它不进行计算以按降序存储它们? 如果用户键入数组的大小为 3,并输入 100、150、433,我希望代码对其进行排序以便输出

  1. 433
  2. 150
  3. 100

而不是打印出来

  1. 100
  2. 150
  3. 433

为什么 for 循环计算不起作用,有没有更好的方法来对数组中的元素进行排序以使它们按降序排列?

这是我的代码,感谢您的帮助:

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

【问题讨论】:

标签: java arrays shift


【解决方案1】:

整数解法:

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

或者你可以使用Arrays.sort(array);,然后反转它。

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

【讨论】:

  • 本质上它不是一个逆序的东西,我只是想让它设置数组中的数字为降序,当数组中有随机的数字分类时。所以 99 151 77 101 将输出为:150 101 99 77
  • @GregPenrose sort 通常按升序排序。 Collections.reverseOrder() 表示比较器将顺序反转为降序。
  • 我注意到阅读 oracle 文档,但我如何使用比较器,我们还没有在讲座中触及这些?
  • @GregPenrose 如果你可以使用Integer[]而不是int[],那么你就不用关心比较器了,因为已经有一个合适的了。
  • 我会使用 Integer 而不是 int。
猜你喜欢
  • 1970-01-01
  • 2011-01-31
  • 2021-11-14
  • 2022-01-05
  • 2021-12-28
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多