Java 程序不';不读取数组中的0

Java 程序不';不读取数组中的0,java,oop,Java,Oop,我试图用java编写一个程序,读取1到100之间的整数,并统计每个整数的出现次数。所以我首先有一个从键盘读取的扫描仪和一个存储数字的数组列表。然后,在WHILE循环中,我编写了两个条件: -仅在“hasNextInt”中写入整数。 -当用户打印0时完成在数组中存储的第二个。 最后,我有一个FOR循环来检查每个数字是否被多次写入。 我的代码是: import java.util.*; public class CountingOccurrenceOfNbs { public static

我试图用java编写一个程序,读取1到100之间的整数,并统计每个整数的出现次数。所以我首先有一个从键盘读取的扫描仪和一个存储数字的数组列表。然后,在WHILE循环中,我编写了两个条件: -仅在“hasNextInt”中写入整数。 -当用户打印0时完成在数组中存储的第二个。 最后,我有一个FOR循环来检查每个数字是否被多次写入。 我的代码是:

import java.util.*;
public class CountingOccurrenceOfNbs 
{
    public static void main(String[] args) 
    {
        int count=1;
        int checker=0;
        Scanner input = new Scanner(System.in);
        System.out.println("Please input your numbers:");

        int num;       // integer will be stored in this variable
        num = input.nextInt();
        ArrayList<Integer> List = new ArrayList<Integer>();
        while (input.hasNextInt()) 
        {
            while(num!=0)
            {
                num = input.nextInt();
                List.add(num);
                for(int i=0; i<List.size(); i++)
                {
                    if(num==checker)
                    {
                        count++;
                        System.out.println(num + " occurs " + count + " times");

                    }
                }
            }
            System.out.println("Array has been filled completly");
        }
        System.out.println("Sorry! You entered a non-Integer value! Try again.");
    }
}   
import java.util.*;
公共类计数NBS的发生
{
公共静态void main(字符串[]args)
{
整数计数=1;
int-checker=0;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入您的号码:”);
int num;//整数将存储在此变量中
num=input.nextInt();
ArrayList=新建ArrayList();
while(input.hasNextInt())
{
while(num!=0)
{
num=input.nextInt();
列表。添加(num);
对于(int i=0;i而言,问题在于:

 if(num==checker)
 {
     count++;
     System.out.println(num + " occurs " + count + " times");
 }
checker
始终为0。因此,它仅在
num
为0时执行

编辑:根据OP的要求

您的代码有多个问题,我只会提到其中的一些问题。首先,在写入一次之前,您通过调用scanner(reading)两次跳过了第一个整数。然后对输入范围没有限制。
checer
未被修改。未到达最后一个打印行

我使用了
HashMap
来存储发生的事件。现在,一旦循环终止,它就会被打印出来,如果你想在循环中,你可以尝试自己实现它。此外,代码依赖于有效的输入。这意味着对于字符串,会出现异常。这就是为什么你可以检查输入是否为整数和thr的某些条件最后,您应该关闭扫描仪并释放内存

import java.util.*;
public class YourClass 
{ 
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input your numbers:");

        int num;       // integer will be stored in this variable
        Map<Integer, Integer> list = new HashMap<Integer, Integer>();
        num = input.nextInt();
        while (input.hasNextInt()){
            while(num != 0){
                if(!list.containsKey(num)){
                list.put(num, 1);
                } else {
                    int value = list.get(num);
                    list.put(num, ++value); //inc. before call, value++ wont work
                }
                num = input.nextInt();
            }
            for ( Map.Entry<Integer, Integer> entry : list.entrySet()) {
                Integer key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(key + " occurs " + value + " times");
            }
            System.out.println("Array has been filled completly");
            System.exit(0);
        }
        //not reached
        System.out.println("Sorry! You entered a non-Integer value! Try again.");
        input.close();
    }
}   
import java.util.*;
公共课你的课
{ 
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入您的号码:”);
int num;//整数将存储在此变量中
映射列表=新的HashMap();
num=input.nextInt();
while(input.hasNextInt()){
while(num!=0){
如果(!list.containsKey(num)){
list.put(num,1);
}否则{
int值=list.get(num);
list.put(num,++value);//inc.在调用之前,value++不起作用
}
num=input.nextInt();
}
对于(Map.Entry:list.entrySet()){
整数key=entry.getKey();
整数值=entry.getValue();
System.out.println(键+“发生”+值+“次数”);
}
System.out.println(“数组已完全填充”);
系统出口(0);
}
//未达到
System.out.println(“对不起!您输入了一个非整数值!请重试。”);
input.close();
}
}   
公共静态void main(字符串[]args){
ArrayList myList=新的ArrayList();
扫描仪=新的扫描仪(System.in);
System.out.println(“请输入您的号码:”);
int sentinel=-1;//键入-1以完成读取输入。
int x;
while((x=scanner.nextInt())!=sentinel)
添加(x);
scanner.close();
//不显示重复频率
Set uniqueInput=newhashset(myList);
for(整数i:唯一输入){
int引用=Collections.frequency(myList,i);
System.out.println(i+“发生”+发生次数+“次数”);
}
}

它正在打印:0出现1次。因此它只运行最后一个数字。我在第一个while循环内有for,在第二个while循环外也在第一个while中有for。当我输入例如:12 12 15 6 0时,它表示所有数字都出现1次。如果我在第一个while外放for,程序不会打印任何内容。我'我是java新手,所以我不知道HashMap和containsKeyI,但是我们可以在基本数组声明上解决这个问题吗?(例如int[]array=newint[value])是的,您可以使用CountingSort算法的第一部分来计算出现次数,然后迭代并仅打印那些“!=0”。首先,您为99个元素[1..100]创建一个数组,然后用0初始化它(在java中,这是自动完成的)。对于读取的数字,您检查数组的索引是否等于,然后将其递增。最后,您将所有索引打印为不同于0的值。对不起,我不明白,您的意思是要先写入int[]myList=new int[99]吗?如果是,这将在此数组中存储100个元素。我希望写入尽可能多的元素。
 public static void main(String[] args) {

    ArrayList<Integer> myList = new ArrayList<Integer>();
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please input your numbers:");

    int sentinel = -1; // Type -1 to finish reading input.
    int x;
    while ((x = scanner.nextInt()) != sentinel)
        myList.add(x);

    scanner.close();

    // To show no duplicate frequency
    Set<Integer> uniqueInput = new HashSet<Integer>(myList);

    for (Integer i : uniqueInput) {

        int occurrences = Collections.frequency(myList, i);

        System.out.println(i + " occurs " + occurrences + " times");

    }

}