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

如何获取数组中最大值对应的序号?Java代码调试求助

问题修复:数组最大值对应天数输出错误

你需要打印数组中最大值对应的天数(序号)及最大值,但现有代码误将数组存储的小时数当作天数输出。例如输入4、9、3、7、6时,预期输出Day #2 with 9 hours worked,实际却输出Day #9 with 9 hours worked。

原代码

import java.util.*;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        Scanner userinput = new Scanner(System.in);
        System.out.print("Enter hours worked on day #1: ");
        int Day1hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #2: ");
        int Day2hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #3: ");
        int Day3hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #4: ");
        int Day4hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #5: ");
        int Day5hours = userinput.nextInt();

        ArrayList<Integer> day = new ArrayList<Integer>();
        day.add(Day1hours);
        day.add(Day2hours);
        day.add(Day3hours);
        day.add(Day4hours);
        day.add(Day5hours);
        double avg;
        int sum = 0;
        int mosthours = Collections.max(day);

        System.out.println("The most hours worked was on: ");
        for (int i = 0; i < day.size(); i++) {
            int daywithmosthours = day.get(i);
            if (day.get(i) == mosthours) {
                System.out.println("Day #" + daywithmosthours + " when you worked " + mosthours + " Hours.");
            }
        }

    }
}

错误原因

在for循环中,你错误地将数组元素(小时数)赋值给了daywithmosthours,导致输出时把小时数当成了天数序号。数组索引从0开始,而天数从1开始,正确的天数应该是索引值+1。

修复后的代码

import java.util.*;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        Scanner userinput = new Scanner(System.in);
        System.out.print("Enter hours worked on day #1: ");
        int Day1hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #2: ");
        int Day2hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #3: ");
        int Day3hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #4: ");
        int Day4hours = userinput.nextInt();
        System.out.print("Enter hours worked on day #5: ");
        int Day5hours = userinput.nextInt();

        ArrayList<Integer> day = new ArrayList<Integer>();
        day.add(Day1hours);
        day.add(Day2hours);
        day.add(Day3hours);
        day.add(Day4hours);
        day.add(Day5hours);
        double avg;
        int sum = 0;
        int mosthours = Collections.max(day);

        System.out.println("The most hours worked was on: ");
        for (int i = 0; i < day.size(); i++) {
            // 用索引+1作为天数序号,不再取数组元素
            int dayNumber = i + 1;
            if (day.get(i) == mosthours) {
                System.out.println("Day #" + dayNumber + " when you worked " + mosthours + " Hours.");
            }
        }

    }
}

说明

修复后,当输入4、9、3、7、6时,代码会正确输出:

The most hours worked was on: 
Day #2 when you worked 9 Hours.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 21:50:22