为什么它总是显示最小值为 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");
}
}
minIndex
超出了[1,12]的范围。使用调试器跟踪程序的流程,看看到底发生了什么。编辑:SALES中最低的值是0,你通过SALES.add(0,000)
添加的。 - kryger