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

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

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

这是我的代码:

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 Months = new HashMap();

public static ArrayList SALES = new ArrayList();

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

}

}

最佳答案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值