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

Java跑步比赛程序按姓名首字母排序功能异常求助

问题根因

你遇到的姓名排序异常和首字母相同场景失效的问题,全部来自姓名排序分支的逻辑书写错误,和compareToIgnoreCase方法本身无关:

  • 当判定runner1为字典序最小的选手后,若runner2字典序小于等于runner3,你错误将infoRunner3放在infoRunner2前面,顺序完全颠倒
  • 当判定runner2为字典序最小的选手后,若runner1字典序大于runner3,你错误将已经放在第一位的infoRunner2重复添加到末尾,漏掉了infoRunner3
  • compareToIgnoreCase本身支持完整字符串的自然排序,首字母相同时会自动对比后续字符,无需额外适配该场景
修正后的完整代码
import javax.swing.JOptionPane;
import java.util.Scanner;

public class Race {
    public static void main (String[] args){
//declaring Variables
        String runner1, runner2, runner3, infoRunner1, infoRunner2, infoRunner3, inputUser, rankOutput;
        int runtime1, runtime2, runtime3;


        inputUser = JOptionPane.showInputDialog("Please enter runner one name: ");
        runner1 = (inputUser);
        inputUser = JOptionPane.showInputDialog("Enter runner one time: ");
        runtime1 = Integer.parseInt(inputUser);
        infoRunner1 = (runner1 + " " + runtime1);
        inputUser = JOptionPane.showInputDialog("Please enter runner two name: ");
        runner2 = (inputUser);
        inputUser = JOptionPane.showInputDialog(" Please enter runner two time ");
        runtime2 = Integer.parseInt(inputUser);
        infoRunner2 = (runner2 +" " + runtime2);
        inputUser = JOptionPane.showInputDialog("Please enter runner three name: ");
        runner3 = (inputUser);
        inputUser = JOptionPane.showInputDialog(" Please enter runner three time ");
        runtime3 = Integer.parseInt(inputUser);
        infoRunner3 = (runner3 +" " + runtime3);
//Naming sort
        String OutputName = "";
        if (runner1.compareToIgnoreCase(runner2) <= 0 && runner1.compareToIgnoreCase(runner3) <= 0) {
            OutputName += infoRunner1 + "\n";
            if (runner2.compareToIgnoreCase(runner3) <= 0) {
                // 修正顺序:runner2比runner3小,先放runner2再放runner3
                OutputName += infoRunner2 + "\n";
                OutputName += infoRunner3 + "\n";
            } else {
                OutputName += infoRunner3 + "\n";
                OutputName += infoRunner2 + "\n";
            }
        }
        else if (runner2.compareToIgnoreCase(runner1) <= 0 && runner2.compareToIgnoreCase(runner3) <= 0) {
            OutputName += infoRunner2 + "\n";
            if (runner1.compareToIgnoreCase(runner3) <= 0) {
                OutputName += infoRunner1 + "\n";
                OutputName += infoRunner3 + "\n";
            } else {
                // 修正错误:原来错误添加runner2,改为添加runner3
                OutputName += infoRunner3 + "\n";
                OutputName += infoRunner1 + "\n";
            }
        }
        else if (runner3.compareToIgnoreCase(runner2) <= 0 && runner3.compareToIgnoreCase(runner1) <= 0) {
            OutputName += infoRunner3 + "\n";
            if (runner2.compareToIgnoreCase(runner1) <= 0) {
                OutputName += infoRunner2 + "\n";
                OutputName += infoRunner1 + "\n";
            } else {
                OutputName += infoRunner1 + "\n";
                OutputName += infoRunner2 + "\n";
            }
        }
//ranking
        String firstplace = "";
        String secondplace = "";
        String thirdplace = "";
        if (runtime1 >= runtime2 && runtime1 >= runtime3){
            firstplace = infoRunner1;}
        else if (runtime1 >= runtime2 && runtime1 <= runtime3){
            secondplace = infoRunner1;}
        else if (runtime1 <= runtime2 && runtime1 <= runtime3){
            thirdplace = infoRunner1;}
        if (runtime2 >= runtime1 && runtime2 >= runtime3){
            firstplace = infoRunner2;}
        else if (runtime2 >= runtime1 && runtime2 <= runtime3){
            secondplace = infoRunner2;}
        else if (runtime2 <= runtime3 && runtime2 <= runtime1){
            thirdplace = infoRunner2;}
        if (runtime3 >= runtime1 && runtime3 >= runtime2){
            firstplace = infoRunner3;}
        else if (runtime3 >= runtime1 && runtime3 <= runtime2){
            secondplace = infoRunner3;}
        else if (runtime3 <= runtime1 && runtime3 <= runtime2){
            thirdplace = infoRunner3;}

        rankOutput  = thirdplace + "\n" + secondplace + "\n" +firstplace + "\n";
        JOptionPane.showMessageDialog(null, "Ranking Order\n" + rankOutput + "\nName Order \n" + OutputName);
        System.exit(0);
    }
}
效果说明

修正后全程未使用数组,符合作业要求,不管选手姓名首字母是否相同,都会按照忽略大小写的字典序正确排序。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 22:54:07