为什么它说最低的数字总是空?

时间:2015-12-14 12:41:40

标签: java null hashmap

为什么说最低的数字总是为空?我很困惑,我试图做出很多改变,但无法看到它的错误。

它为我提供了正确的最高数字,但是对于最低的数字,它始终在控制台中打印:最低销售额为空

这是我的代码:

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");


    }

}

2 个答案:

答案 0 :(得分:1)

Java索引从0开始。

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

并更改这些

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

答案 1 :(得分:0)

SALES.add(0,000);

第0个月的最低销售额为0,在地图中没有值。

因此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。