You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写Java程序:输入5个评分并按从高到低输出编号及最值

Java评分排序与最值编号输出程序修正

需求说明

编写程序接收5个评分输入,按从高到低顺序输出对应评分编号;同时判断并输出最高、最低评分的编号及数值。

测试用例

输入

Rate 1: 237
Rate 2: 4303
Rate 3: -635
Rate 4: 715
Rate 5: 56

预期输出

Ratings in Highest to Lowest order :

Rate 2
Rate 4
Rate 1
Rate 5
Rate 3

The Highest Rating is Rate 2 with the value of 4303
The Lowest Rating is Rate 3 with the value of -635

用户尝试的代码

try {
    //Create Object for Scanner class to get input from keyword
    Scanner g = new Scanner(System.in);
    System.out.print("Enter the value of N: ");
    //Declaring array
    int inputValue = g.nextInt(), temp;
    //Get input from keyword
    System.out.print("Input Rating Value Below\n");
    int[]arr = new int[inputValue];
    //Get all input from keyword
    for(int i = 0, j= 1; i <= inputValue && j <= inputValue;i++, j++){
        System.out.print("Rate " + j + ":");
        arr[i]= g.nextInt();
    }
    //Sort the given ratings
    for(int m=0;m<arr.length;m++) {
        for(int n=m+1;n<arr.length;n++) {
            if(arr[m]<arr[n]) {
                temp = arr[m];
                arr[m]=arr[n];
                arr[n]=temp;
            }
        }
    }
    //Output of sorted ratings
    System.out.print("Ratings in Highest to Lowest: \n");
    for(int t: arr) {
        System.out.println("Rate : " + t);
    }
    //assign first element of an array to largest and smallest
    int min = arr[0];
    int max = arr[0];

    for (int i = 0; i < arr.length; i++) {
        if (arr[i] > max)
            max = arr[i];
        else if (arr[i] < min)
            min = arr[i];
    }

    System.out.println("The Highest Rating is "+ max);
    System.out.println("The Lowest Rating is " + min);
}

原代码问题分析

  1. 丢失编号信息:仅存储评分数值,排序后无法对应原始编号,导致输出时无法显示Rate X格式
  2. 循环越界错误:输入循环条件i <= inputValue会触发数组索引越界(数组索引从0开始,最大索引为inputValue-1)
  3. 多余输入步骤:需求固定接收5个评分,无需让用户输入N
  4. 输出格式不符:排序结果仅打印数值,未按要求输出Rate X;最值输出缺少编号和正确格式
  5. 冗余逻辑:排序后数组首元素就是最大值、尾元素就是最小值,无需再遍历判断最值

修正后的代码

import java.util.Scanner;

public class RatingSorter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 二维数组存储[编号, 评分],固定处理5个评分
        int[][] ratings = new int[5][2];

        // 输入5个评分并关联编号
        for (int i = 0; i < 5; i++) {
            int rateNumber = i + 1;
            System.out.print("Rate " + rateNumber + ": ");
            int score = scanner.nextInt();
            ratings[i][0] = rateNumber; // 存储编号
            ratings[i][1] = score;      // 存储评分值
        }

        // 按评分从高到低排序,交换整行以保留编号与评分的对应关系
        for (int m = 0; m < ratings.length; m++) {
            for (int n = m + 1; n < ratings.length; n++) {
                if (ratings[m][1] < ratings[n][1]) {
                    int[] temp = ratings[m];
                    ratings[m] = ratings[n];
                    ratings[n] = temp;
                }
            }
        }

        // 输出排序后的编号
        System.out.println("\nRatings in Highest to Lowest order :\n");
        for (int[] rate : ratings) {
            System.out.println("Rate " + rate[0]);
        }

        // 从排序后的数组直接获取最值信息
        int highestRateNum = ratings[0][0];
        int highestScore = ratings[0][1];
        int lowestRateNum = ratings[4][0];
        int lowestScore = ratings[4][1];

        // 输出最值详情
        System.out.println("\nThe Highest Rating is Rate " + highestRateNum + " with the value of " + highestScore);
        System.out.println("The Lowest Rating is Rate " + lowestRateNum + " with the value of " + lowestScore);

        scanner.close();
    }
}

修正说明

  1. 使用二维数组绑定评分编号与数值,确保排序时两者始终关联,不会丢失对应关系
  2. 固定处理5个评分,去掉多余的N输入步骤,贴合需求
  3. 排序时交换整个数组行,避免编号与评分脱节
  4. 严格按照预期格式输出,包括排序后的编号列表和最值的完整描述
  5. 直接从排序后数组的首尾获取最值,简化逻辑提升效率

内容的提问来源于stack exchange,提问作者Hiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 00:10:50