java - 为什么它总是说最低的数字是 Null?

标签 java null hashmap

为什么说最小的数字总是Null?我很困惑,我尝试进行很多更改,但看不出有什么问题。

它给了我正确的最高数字,但对于最低的数字,它总是在控制台中打印:Lowest sales were in 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");


    }

}

最佳答案

Java 索引从 0 开始。

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

并改变这些

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

关于java - 为什么它总是说最低的数字是 Null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34267338/

相关文章:

java - 如何构建全局序列比对的评分矩阵?

计算字符串长度c

java - 如何在java8中的JavaScript引擎中使用java对象

java - 如何创建一个包含字符串和对象哈希表条目的 JList?

java - java中的序列比较

Java多线程——加入一个CPU繁重的线程和volatile关键字

php - 在 PHP 中通过空引用传递

java - Hashmap get 函数返回 null

java - 读取 CSV 记录,同时忽略尾随空格

java - Parcelable 对象在接收 Activity 中为 Null,在其他 Activity 中为 Good