gpt4 book ai didi

java - 当尝试对相同的值进行两次计数时,循环不正确且超出 HashMap 的范围

转载 作者:行者123 更新时间:2023-12-02 10:37:58 25 4
gpt4 key购买 nike

下面的代码接受用户输入(int 值),然后查找重复、偶数、数量。

2 个问题:

  1. The 1st even number gets printed 3 times, if in your input you have 2 7 19 8 it will look like 2, 2, 2, 8 . Why I am unable to
    understand.
  2. The hashmap part where I put list.get(intNum)+1 to count a value twice gives out of bound. Without that it runs. How can I count a number twice?

提前致谢。

public class Various {

static Scanner userinput= new Scanner(System.in);
static int nums;
static int big;//will be used to find bug number in array
public static void main(String[] args) {

List<Integer> list= new ArrayList<>();//for all numbers
List<Integer> listEven= new ArrayList<>(); //just for evens
Map<Integer,Integer> hmap= new HashMap<Integer,Integer>(); //number count

System.out.println("Enter some numbers: ");
nums=userinput.nextInt();
big=nums;
int evenNumber;

list.add(nums);

while(userinput.hasNextInt()){
nums=userinput.nextInt();

//below part of the code finds biggest value
if(nums>big){
big=nums;
}
//Above part of the code finds biggest value

list.add(nums);

//below part of the code finds/prints duplicate value
for(int i=0;i<list.size()-1;i++){
for(int j=i+1;j<list.size();j++){
if(list.get(i)==list.get(j)){
System.out.println("Duplicate " + list.get(j));
}
}
//Above part of the code finds/prints duplicate value
}
//below part of the code finds even numbers
for(int i=0;i<list.size();i++){
//for loop will start from 0 till user input
if(list.get(i)%2==0){
//if any of those i values%2=0 those will be Even and I will capture those
listEven.add(list.get(i));
}
//Above part of the code finds even numbers
}
//below part of the code finds number occurrence count
for(Integer intNum: list){
if(!(hmap).containsKey(intNum)){
hmap.put(intNum, 1);
}else{
//below part to take care if a number comes twice i add 1 more(+1)
hmap.put(intNum, list.get(intNum)+1);
}
//Above part of the code finds number occurrence count
}
}
System.out.println("Values are " + list);
System.out.println("Biggest value " + big);
System.out.println("Even numbers " + listEven);
System.out.println("Numbers in list and occurance " + hmap);
}
}

最佳答案

问题A:

在您的代码中,您有一个 while 循环来检查数字,但每次用户输入数字时,您都会循环遍历数字列表并检查偶数。

最合乎逻辑的方法是更改​​代码,以便在用户输入所有号码后检查偶数。或者您可以添加这个,这是一个较小的解决方案,但它会起作用:

<小时/>
listEven.add(list.get(i));

将上面的代码更改为下面的代码。

if (!listEven.contains(list.get(i))) {
listEven.add(list.get(i));
}

问题B:

错误在于这段代码:

// below part of the code finds number occurrence count
for (Integer intNum : list) {
if (!(hmap).containsKey(intNum)) {
hmap.put(intNum, 1);
} else {
// below part to take care if a number comes twice i add 1
// more(+1)
hmap.put(intNum, list.get(intNum) + 1);
}
// Above part of the code finds number occurrence count
}
<小时/>

您正在尝试执行list.get(intNum),但是方法get需要索引,而不需要 您要查找的值。我认为您的意思是输入 hmap.get(intNum) 而不是 list.get(intNum),这将正确更新计数。

关于java - 当尝试对相同的值进行两次计数时,循环不正确且超出 HashMap 的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53142929/

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