【问题标题】:How to check whether the key number is repeated in the array?如何检查数组中键号是否重复?
【发布时间】:2019-05-21 06:14:59
【问题描述】:

如何从 key(b) 中获取重复的数字我的程序是这样的,用户输入 166456 并且 key = 6 那么输出必须像 6 在数组中重复 3 次请告诉我这是否可以没有数组完成
我什至收到错误 int cannot be to int[]

    int []a,i,j,count=0,b=0,n;
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt(System.in);
    int []a=new int[n];
    for(i=0;i<n;++i)
    {
        a[i]=sc.nextInt();

    }
    System.out.println("Which nuber would you like to find:");
    b=sc.nextInt();
    for(j=0;j<n;++j)
    {
        if(a[i]==b)
        {
            ++count;
        }
        else
        {
            System.out.println("Not found");
        }
    }
    System.out.println("No of time "+b+" is repeated "+count);

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    您正在做一些错误的变量声明。如果您将数组声明为int []a,那么它会将所有变量视为一个数组。这就是您收到错误的原因。您可以声明为int a[],也可以在不同的行上声明其他变量。

    请参考以下代码,

    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String[] args) {
            int []a;
            int b=0, count=0;
            int i, j, n;
            Scanner sc=new Scanner(System.in);
            n=sc.nextInt();
            a=new int[n];
            for(i = 0; i < n; ++i)
            {
                a[i]=sc.nextInt();
    
            }
            System.out.println("Which nuber would you like to find:");
            b=sc.nextInt();
            for(j=0;j<n;++j)
            {
                if(a[j]==b)
                {
                    ++count;
                }
                else
                {
                    System.out.println("Not found");
                }
            }
            System.out.println("No of time "+b+" is repeated "+count);
    
        }
    
    }
    

    输出

    6
    1
    6
    6
    4
    5
    6
    Which nuber would you like to find:
    6
    Not found
    Not found
    Not found
    No of time 6 is repeated 3
    

    【讨论】:

    • 我仍然没有得到重复数字的计数
    • @Ragnarok 我可以看到输出。我已经用输出更新了我的答案。请检查。
    • 为什么它的显示 Not found as 6 存在于数组中??
    • @Ragnarok 这是因为您在每次迭代中打印值。如果值为 6,那么它将增加计数,否则它将打印未找到。如果您不想显示 then,请删除 else 块。
    【解决方案2】:

    据我了解要求,您需要有关从用户输入中找出数字的总频率的帮助。 (假设数字只是从 0 到 9。如果我在这个假设上错了,请纠正我)。

    因此,为此,我们可以只使用大小为 10 的整数数组来存储每个数字的频率。例如,考虑总共 6 个数字的输入 - “166456”。我们的整数数组的值将是(从索引 0 到 9)0100113000。因此,我们可以直接返回要搜索的数字的索引,在本例中返回数组 [6] 的值为 3。

        int[] num=new int[10]; // storing each digit's frequency
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); // count for total digits
        for(int i=0;i<n;i++){
            int index = sc.nextInt();
            num[index]++;
        }
        System.out.println("Which number you would like to find out?");
        int b = sc.nextInt();
        if(num[b]!=0)
            System.out.println("No. of time "+b+" is repeated "+num[b]);
        else
            System.out.println("Not found");
    

    【讨论】:

    • 它给了我错误,它不会检索想要任何限制的数字,甚至我不想将它存储在数组中,只需使用键找到重复元素的数量
    • 好吧,我只是运行它并显示输出。我不确定您为什么会收到错误消息。我们也可以在这里使用 HashMap 。这也可以帮助你。
    • 嗨,如果用户直接输入数字,而不是像上面那样选择存储,如果用户直接输入 16648 并且键值必须为 6,那么是否可以这样做直接获得 2 次作为其重复次数
    • 因此,根据您的要求,如果您想将 16648 视为一个整数,并且通过将键值提供为 6,您希望输出为 2,那么您可以将数字转换为字符串然后在整个字符串中查找关键数字的出现,或者您可以通过除以 10 并取模来提取每个数字。
    猜你喜欢
    • 2016-08-28
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2013-11-14
    • 2011-11-14
    相关资源
    最近更新 更多