为什么显示最低数字总是为空?

4

为什么它总是显示最小值为 Null?我很糊涂,我试图做出很多更改,但看不出有什么问题。

它给了我正确的最大数字,但对于最小数字,它总是在控制台打印出:"最低销售额为null"

这是我的代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;


public class Project5 {

    /**
     * @param args
     *//****************************************************
     * Filename:       jdoe2pg.c                               *
     * Name:           Ima Student                             *
     * SSAN:           6789                                    * 
     * Date:           13 December 2005                        *
     * Course:         CMSC-104                                * 
     * Description:    (Your psuedocode for main() goes here)  *
     *     Analysis:                                           *
     *                 Input:                                  *
     *                 Output:                                 *
     *                 Formulas:                               *
     *                 Constraints:                            *
     *                 Assumptions:                            *
     *     Design:     (Psuedocode is here)                    *
     * Notes:          (As needed, such has how to compile)    *
     ***********************************************************/


    public static Scanner in = new Scanner(System.in);
    public static HashMap<Integer, String> Months = new HashMap<Integer, String>();
    public static ArrayList<Integer> SALES = new ArrayList<Integer>();
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SALES.add(0,000);
        addHash();

        for(int i =1;i<=12;i++){
        System.out.println("Enter sales for " + Months.get(i));
        int value= in.nextInt();
        SALES.add(i,value);
        }

        //Get Max
        int max = Collections.max(SALES);
        int maxIndex = SALES.indexOf(max);
        System.out.println("Highest sales were in " + Months.get(maxIndex));

        //Get Min
        int min = Collections.min(SALES);
        int minIndex = SALES.indexOf(min);
        System.out.println("Lowest sales were in " + Months.get(minIndex));

        //Gets all the sales
        for(int i=1;i<=12;i++){
        System.out.println(Months.get(i) + ": " + SALES.get(i));
        }
    }

    public static void addHash(){

        Months.put(1,"January");
        Months.put(2,"Feburary");
        Months.put(3,"March");
        Months.put(4,"April");
        Months.put(5,"May");
        Months.put(6,"June");
        Months.put(7,"July");
        Months.put(8,"August");
        Months.put(9,"September");
        Months.put(10,"October");
        Months.put(11,"November");
        Months.put(12,"December");


    }

}

3
这意味着minIndex超出了[1,12]的范围。使用调试器跟踪程序的流程,看看到底发生了什么。编辑:SALES中最低的值是0,你通过SALES.add(0,000)添加的。 - kryger
可能与“月份”被1索引有关。 - Mohammed Aouf Zouag
那么最大值是如何工作的?@Sparta? - user5613751
@kryger 那我该怎么办呢 :( 我基本上已经完成了,但不知道如何解决低的问题。 - user5613751
@AmanuelBogale,请删除“SALES.add(0,000);”这一行。 “我基本上完成了它”:你只有在“基本完成”时才检查程序的正确性吗?编辑:...然后继续验证您的其余代码。 - kryger
显示剩余2条评论
2个回答

1

Java索引从0开始。

SALES.add(0,000); //remove

并更改这些

SALES.add(value);
System.out.println(Months.get(i) + ": " + SALES.get(i - 1));

非常感谢!只是想问一下,是否有类似于 Collections.Average 的东西? - user5613751

0
SALES.add(0,000);

您的最低销售额是0,对应月份为0,在您的Map中没有值。

因此Months.get(minIndex)返回null。

您不必为索引0添加值为0的数据。

    // SALES.add(0,000); remove this
    addHash();

    for(int i = 0;i< 12;i++){ // iterate from 0 to 11
        System.out.println("Enter sales for " + Months.get(i+1)); // add 1 when you need
                                                                  // to obtain month name
        int value= in.nextInt();
        SALES.add(i,value); // or simply SALES.add(value);
    }

    //Get Max
    int max = Collections.max(SALES);
    int maxIndex = SALES.indexOf(max);
    System.out.println("Highest sales were in " + Months.get(maxIndex+1));

    //Get Min
    int min = Collections.min(SALES);
    int minIndex = SALES.indexOf(min);
    System.out.println("Lowest sales were in " + Months.get(minIndex+1));

    //Gets all the sales
    for(int i=0;i<12;i++){
        System.out.println(Months.get(i+1) + ": " + SALES.get(i));
    }

当然,你可以将你的Months映射的键更改为从0到11,这样每当你调用Months.get()时就不必再添加1到索引中了。

好的,当我将0索引设置为高时,变成高位的那个变量会变成null。当它变成null时,低位的变量也会变成null。我该怎么修复它? - user5613751
@AmanuelBogale,我不明白你的评论。我发布的代码应该可以工作。你试过了吗? - Eran
非常感谢!只是想问一下,是否有像Collections.Average这样的东西?只是问一下,但还是谢谢。 - user5613751
1
@AmanuelBogale 你可以编写简单的代码来计算平均值。如果你使用Java 8,你可以查看IntSummaryStatistics,它可以用于获取IntStream的平均值(以及其他内容),但你需要学习Streams相关知识。 - Eran
好的,非常感谢,@Eran! - user5613751

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接