gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:35:11 26 4
gpt4 key购买 nike

为什么说最小的数字总是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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com