Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试计算数组中出现的次数_Java_Arrays_Count_Find Occurrences - Fatal编程技术网

Java 尝试计算数组中出现的次数

Java 尝试计算数组中出现的次数,java,arrays,count,find-occurrences,Java,Arrays,Count,Find Occurrences,所以我试图计算这个程序中出现的整数的数量。代码仍然不起作用,但我走对了吗 public static void main(String[] args) { Scanner scan = new Scanner(System.in); int userInput = 0; ArrayList<Integer> myList = new ArrayList<Integer>(); int[]

所以我试图计算这个程序中出现的整数的数量。代码仍然不起作用,但我走对了吗

    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);

        int userInput = 0;
        ArrayList<Integer> myList = new ArrayList<Integer>();
        int[] newArray = new int[myList.size()];
        int index1 = -1;
        int current;

        for (int num = 0; num <= userInput ; num++)
        {   
            System.out.println("Please enter a random number between 0 and 50, enter a negative number to end input: ");
            num--;

            if(userInput >= 0 || userInput <= 50)
            {       
                userInput++;
                userInput = scan.nextInt();
                index1++;
                myList.add(userInput);

            }
            if (userInput < 0 || userInput > 50)
            {
                myList.remove(index1);
                index1--;
                break;
            }
        }   

        for (int num2 = 0; num2 < myList.size(); num2++)
            {
                current = myList.get(num2);
                if(current == myList.get(num2))
                {
                    newArray[current-myList.get(num2)]++;
                }
            }

            for (int number=0; number < newArray.length; number++)
            {
                System.out.println(number + "1");
                System.out.println(" : " + newArray[number]);
            }
}
}
publicstaticvoidmain(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
int userInput=0;
ArrayList myList=新的ArrayList();
int[]newArray=newint[myList.size()];
int index1=-1;
电流;
对于(int num=0;num=0 | | userInput 50)
{
myList.remove(index1);
指数1--;
打破
}
}   
对于(int num2=0;num2
编辑:
我只是想补充一点,我可以运行这个程序,但是当我输入一个超出边界的整数(不是0到50之间)时,我得到了一个错误,我刚刚写完这篇文章,感谢我修改代码时的否决票,并特别声明我正在修改代码。Hashmaps将在这里为您实现这一点

public static void main(String args[]) {
        HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();
        Scanner scan = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("Number: ");
            int current = scan.nextInt();
            if (current > 0 && current < 51) {
                if (numbers.containsKey(current)) {
                    numbers.put(current, numbers.get(current)+1);
                }else{
                    numbers.put(current, 1);
                }
            } else {
                if(numbers.isEmpty()){
                    System.out.println("Nothing inputted");
                    loop = false;
                }else{
                    for(Entry<Integer,Integer> e : numbers.entrySet()){
                        System.out.println(e.getKey() + " was entered " + e.getValue() + " times");
                    }
                    loop = false;
                }
            }
        }
    }
publicstaticvoidmain(字符串参数[]){
HashMap number=新的HashMap();
扫描仪扫描=新扫描仪(System.in);
布尔循环=真;
while(循环){
System.out.println(“编号:”);
int current=scan.nextInt();
如果(电流>0&&电流<51){
if(编号。容器(当前)){
数字.put(当前,数字.get(当前)+1);
}否则{
数字。输入(当前,1);
}
}否则{
if(numbers.isEmpty()){
系统输出打印项次(“无输入”);
循环=假;
}否则{
对于(条目e:numbers.entrySet()){
System.out.println(e.getKey()+“已输入”+e.getValue()+“次”);
}
循环=假;
}
}
}
}

您不需要任何像myList这样的附加集合。 您可以声明数组

int[] occurrences = new int[51];
它可以存储[0,50]范围内元素的出现。当您从扫描仪中读取n作为下一个字符串时,如果n在范围内,则应在readedn的第四个位置增加此数组的元素,否则应停止读取新值

如果statemest始终为true,则此选项为:

current = myList.get(num2);
if(current == myList.get(num2))
代码示例:

public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int[] occurrrences = new int[51];

        while (true){
            int value = scanner.nextInt();
            if(value>=0 && value<occurrrences.length){
                occurrrences[value]++;
            }else{
                scanner.close();
                break;
            }
        }
        for (int i = 0; i < occurrrences.length; i++) {
            if(occurrrences[i]>0){
                System.out.println(i + " occured " + occurrrences[i] + " times.");
            }
        }
    }
publicstaticvoidmain(字符串[]args){
扫描仪=新的扫描仪(System.in);
int[]发生率=新int[51];
while(true){
int value=scanner.nextInt();
如果(值>=0&&value0){
System.out.println(i+“发生”+发生次数[i]+“次数”);
}
}
}

问题出在哪里?想想这一行:
int[]newArray=newint[myList.size()]-提示:您正在创建一个空数组。甚至不会进入for循环
int userInput=0;for(int num=0;num