计算数组中的次数(Java) [英] Counting an Occurrence in an Array (Java)
问题描述
我完全被难过了。我休息了几个小时,我似乎无法想出这个。令人沮丧!
I am completely stumped. I took a break for a few hours and I can't seem to figure this one out. It's upsetting!
我知道我需要检查数组中的当前元素,看它是否出现在数组的其他位置。想法是输出以下内容:
I know that I need to check the current element in the array and see if it appears elsewhere in the array. The idea is to output the following:
要求用户输入10个整数,并将这些整数分配给一个数组(因此数字作为一个参数用于方法)。假设我输入1,1,2,3,3,4,5,6,7,8。打印结果应为1次发生2次.2次发生1次.3次发生2次.4次发生1次.5次发生1次.6次发生1次.7次发生1次.8次发生1次。此打印将以单独的方法完成。
The user is asked to enter 10 integers and those integers are assigned to an array (hence the "numbers" as a paremeter for the method). Let's say I enter "1, 1, 2, 3, 3, 4, 5, 6, 7, 8." The printed results should be "1 occurs 2 times. 2 occurs 1 time. 3 occurs 2 times. 4 occurs 1 time. 5 occurs 1 time. 6 occurs 1 time. 7 occurs 1 time. 8 occurs 1 time." This printing will be done in a separate method.
我的代码中的所有内容都有效,除了我为计算出现次数而创建的方法。
Everything in my code works except for this method that I created to count the occurrences.
public static int getOccurrences(int[] numbers)
{
int count = 0;
for (int i = 0; i < numbers.length; i++)
{
int currentInt = numbers[i];;
if (currentInt == numbers[i])
{
count++;
}
}
return count;
}
我知道这里的问题是什么。我将数组中的当前整数元素设置为变量currentInt。 if语句计算数组中的每个整数元素,因此输出为[I @ 2503dbd3发生10次。
I know what the issue is here. I set the current integer element in the array to the variable currentInt. The if statement counts every integer element in the array, hence the output being "[I@2503dbd3 occurs 10 times."
如何跟踪数组中每个元素的出现?
How do I keep track of the occurrences of each element in the array?
推荐答案
package countoccurenceofnumbers;
import java.util.Scanner;
public class CountOccurenceOfNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int [] num = new int[100];
int [] count = new int[100];
//Declare counter variable i
//and temp variable that will
//temporarily hold the value
//at a certain index of num[] array
int i,temp = 0;
System.out.println("Enter the integers between 1 and 100: ");
//Initialize num[] array with user input
for(i=0; i < num.length; i++){
num[i] = input.nextInt();
//expected input will end when user enters zero
if(num[i] == 0){
break;
}
}//end of for loop
//value at a given index of num array
//will be stored in temp variable
//temp variable will act as an index value
//for count array and keep track of number
//of occurences of each number
for(i = 0; i < num.length; i++){
temp = num[i];
count[temp]++;
}//end of for looop
for(i=1; i < count.length; i++){
if(count[i] > 0 && count[i] == 1){
System.out.printf("%d occurs %d time\n",i, count[i]);
}
else if(count[i] >=2){
System.out.printf("%d occurs %d times\n",i, count[i]);
}
}//end of for loop
}//end of main
}//end of CountOccurrenceOfNumbers
/////////// OUTPUT /////////// ///////////
///////////OUTPUT//////////////////////
输入1到100之间的整数:
2 5 6 5 4 3 23 43 2 0
2发生2次
3发生1次
4发生1次
5
发生2次
6次发生1次
23发生1次
43
发生1次
BUILD SUCCESSFUL(总时间:3分23秒)
Enter the integers between 1 and 100:
2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
BUILD SUCCESSFUL (total time: 3 minutes 23 seconds)
这篇关于计算数组中的次数(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!