简体   繁体   English

确定数字出现多少次-Java

[英]Determine how many times the number appeared - Java

I'm having a problem with this program that I'm making. 我正在制作的程序有问题。 Here's the purpose of the program: Write a program that will get number inputs from the user. 该程序的目的是:编写一个将从用户那里输入数字的程序。 If the user inputs a negative number, the program will stop accepting inputs from the user. 如果用户输入负数,则程序将停止接受来自用户的输入。 Then, the program will find repeating numbers from the program and determine how many times it appeared. 然后,程序将从程序中找到重复的数字,并确定它出现了多少次。

Here's the sample output: 这是示例输出:

    Enter a number : 5
    Enter a number : 5
    Enter a number : 7
    Enter a number : 2
    Enter a number : 7
    Enter a number : 3

    Numbers Entered:
    2   _________ 1
    3   _________ 1
    5   _________ 3
    7   _________ 2

Note: If the number appeared two or more times, it will not duplicate in the output. 注意:如果该数字出现两次或两次以上,则在输出中将不会重复。 The numbers are also arranged. 数字也被安排。

I already figured out first part (if the user inputs a negative number) 我已经弄清楚了第一部分(如果用户输入一个负数)

Scanner scan = new Scanner(System.in);
ArrayList<Integer> arrayNumbers = new ArrayList<Integer>();
ArrayList<Integer> countArray = new ArrayList<Integer>();
int x,;
int counter=0;
int confirm;
int length=0;
int input;
int z;
int elements=0;
for (x=0;x<=counter;x++)
{
    if (counter==x)
    {
        System.out.print("Enter a number : ");
        input = scan.nextInt();
        arrayNumbers.add(input);
        confirm = input;
        if (confirm<0)
        {
            counter--;
            arrayNumbers.remove(x);
        }
        else
        {
            counter++;
            length++;
        }
    }
}

I am now stuck with the comparing part of the program. 我现在停留在程序的比较部分。 I've done a code, but it doesn't seem to work: 我已经完成了代码,但似乎无法正常工作:

int blah,count,z10;
Arrays.sort(finalArray);
for (int i=0;i<finalArray.length;i++)
{
    blah = finalArray[i];
    count=0;
    while(finalArray[i]==blah&&z10<finalArray.length)
    {
        count++;
        i++;
    }
    System.out.println("Number : "+blah+" Count : "+count);     
}

Can someone please help me with the logic or the code? 有人可以帮助我提供逻辑或代码吗? Help will be much appreciated. 帮助将不胜感激。 Thanks :) 谢谢 :)

PS I'm required to do this using Arrays only. PS我只需要使用数组来做到这一点。 No other methods. 没有其他方法。

you should user a HashMap< int,int > where you store the number and the number of times the user entered it 您应该使用HashMap <int,int>来存储数字和用户输入它的次数

try something like this (this is a basic idea not the real code): 尝试这样的事情(这是一个基本的想法,而不是真正的代码):

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

when the user enters the number do something like this: 当用户输入数字时,请执行以下操作:

if (map.containsKey(input)) {
  int counter = map.get(input) + 1;
  map.put(input, counter)
}

Okay, I read over the question in more detail, there are some things I want you to think about. 好的,我更详细地阅读了这个问题,我想让您考虑一些事情。 And since this is obviously a school project, I'll try to give you hints on what you need to do. 而且由于这显然是学校的项目,因此我将尝试为您提供所需的提示。

First, the negative number part you mention. 首先,您提到的负数部分。 You're doing a for loop and comparing against a counter that you're increasing with each number added. 您正在执行for循环,并与添加的每个数字所增加的计数器进行比较。 You add each number to the array and if the last number added was negative, you remove it and reduce the counter that ends the for loop. 将每个数字添加到数组中,如果最后添加的数字为负数,则将其删除并减少结束for循环的计数器。 While this works this is not a good readable code. 尽管这行之有效,但这并不是一个很好的可读性代码。 You should look into using a while loop that checks the input, the pseudocode would be somewhat like this: 您应该考虑使用一个while循环来检查输入,伪代码有点像这样:

int input
array numberArray

read number into input from user
while input >= 0:
    add input to numberArray
    read number into input from user

This will read the number from the user and if it is not negative it will add to the array and read a number again, then repeating the input read from the user as long as it's not negative. 这将从用户读取数字,如果它不是负数,它将添加到数组中并再次读取一个数字,然后重复从用户读取的输入,只要它不是负数即可。

In the latter part you mention you need help with you'll have a sorted array of numbers and you want to print out how many of each number there is in the array you could do it similar to how you are doing it. 在后面的部分中,您提到需要帮助,您将获得一个排序的数字数组,并且想要打印出数组中每个数字有多少个,您可以执行类似的操作。 With the limitations you have (and my limited java knowledge) I would probably do someting like this pseudocode shows: 考虑到您的局限性(以及我有限的Java知识),我可能会做一些类似此伪代码所示的操作:

sort numberArray
int currentNumber = -1
int counter

for i = 0 while i < array.length do i++:
    if numberArray[i] is currentNumber:
        counter++
    else //if new number
        print how many times currentNumber has been encountered (before assigning currentNumber to a new number)
        currentNumber = numberArray[i]
        counter = 1
print how many times currentNumber has been encountered

This should loop through the whole array, count how many times in a row a number is in the array and when you find a new number, you'll print out the current tally, select the new number and set the counter to 1. The reason for the print after the loop is that when we hit the final number in the array, we wont go into the else branch of the if/else where we had our print statement. 这应该遍历整个数组,连续计数一个数组中的一个数字,当您发现一个新数字时,您将打印出当前计数,选择​​新数字并将计数器设置为1。循环后进行打印的原因是,当我们击中数组中的最后一个数字时,我们将不会进入if / else的else分支,那里是我们的打印语句。

I hope this gives you some idea on how you could do it. 我希望这能使您对如何做到这一点有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM