该数字在一行中出现了多少次(Java)

该数字在一行中出现了多少次(Java),java,Java,我应该创建一个main方法来计算一个数字在一行中出现的次数。用户应该输入数字。 我想我必须循环一个数组,虽然我不知道如何 用户输入到数组,循环并检查该数字是否为上一个 例如: 1 3 4 5 6 6 9-输出:数字6连续出现3次 这是我现在拥有的代码(sry没有粘贴它): publicstaticvoidmain(字符串[]args) { 扫描仪inp=新扫描仪(System.in); int计数器=0; int[]x=新的int[inp.nextInt()]; 对于(int i=0;i

我应该创建一个main方法来计算一个数字在一行中出现的次数。用户应该输入数字。 我想我必须循环一个数组,虽然我不知道如何

用户输入到数组,循环并检查该数字是否为上一个

例如: 1 3 4 5 6 6 9-输出:数字6连续出现3次

这是我现在拥有的代码(sry没有粘贴它):

publicstaticvoidmain(字符串[]args)
{   
扫描仪inp=新扫描仪(System.in);
int计数器=0;
int[]x=新的int[inp.nextInt()];
对于(int i=0;i
有什么想法吗?

使用
HashMap
跟踪每个数字的出现次数:

Map<Integer, Long> numberOccurences = new HashMap<>();
for(int n : numbers) {
    Long occurences = numberOccurences.get(n);
    if(occurrences == null) {    // first time the number n is seen
        numberOccurences.put(n, 1L);
    } else {
        numberOccurences.put(n, occurences + 1);
    }
}
Map numberroccurences=newhashmap();
用于(整数n:数字){
长期发生率=numberOccurences.get(n);
如果(出现次数==null){//第一次看到数字n
放药量(n,1L);
}否则{
NumberCurences.put(n,事件+1);
}
}

我们不会为您编写代码。如果您尝试了它,但遇到了问题,如果您尝试了它并希望得到帮助,使它在工作后变得更有效,那么您可以将代码发布在codereview.stackexchange.com您的示例输入的输出应该是什么?@Michelle“数字6连续出现三次”
Map<Integer, Long> numberOccurences = new HashMap<>();
for(int n : numbers) {
    Long occurences = numberOccurences.get(n);
    if(occurrences == null) {    // first time the number n is seen
        numberOccurences.put(n, 1L);
    } else {
        numberOccurences.put(n, occurences + 1);
    }
}