问题是:编写一个程序,读取1-100之间的整数,并计算每个的出现次数。假设输入以0结束。如果数字出现不止一次,则在输出中使用多个“次”。以下是该程序的示例运行:
2次发生2次
3次发生1次
4次发生1次
5次发生2次
6发生1次
23发生1次
43次出现
我已经修复了我的代码中的读取整数,不再是我,而是一个单独的变量“index”,并理解为什么我收到了out of bounds异常,但我有点厚,不明白如何修复它同时添加一个哨兵值0。
import java.util.Scanner;
public class countNumberOccurrence{
public static void main(String args[]){
int[] numbers = new int[100];
inputArray(numbers);
}
public static void inputArray(int[] myList){
Scanner input = new Scanner(System.in);
System.out.print("Enter integers from 1-100 (input 0 value to end inputs): ");
int index = 0;
for(int i = 1; i < myList.length - 1; i++){
if(i > 0){
index = input.nextInt();
myList[index-1]++;
}
if(myList[index-1] > 1)
System.out.println(index + " occurs " + myList[index-1] + " times ");
else
System.out.println(index + " occurs " + myList[index-1] + " time ");
}
}
}
答案 0 :(得分:4)
检测输入0
并结束应用程序的正确逻辑是:
int nextInt;
while ((nextInt = input.nextInt()) != 0)
{
// do your logic here
}
// print your output here
这是处理输入的正确方法。
始终使用相应的java.util.collections
类并使用for/each
或正确的Iterator
进行迭代,您将不会出现这些错误。
以下是仅限JDK的解决方案:
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
public class Q21871053
{
public static void main(final String[] args)
{
final Random rnd = new Random();
final Map<Integer, AtomicInteger> counter = new HashMap<Integer, AtomicInteger>()
{
@Override
public AtomicInteger get(final Object key)
{
if (!super.containsKey(key)) { super.put((Integer) key, new AtomicInteger(rnd.nextInt(100))); }
return super.get(key);
}
};
final AtomicInteger nextInt = new AtomicInteger(rnd.nextInt(100));
while (nextInt.get() != 0)
{
counter.get(nextInt.get()).incrementAndGet();
if (counter.size() > 1000) { nextInt.set(0); } // limit the run to 1000 numbers
else { nextInt.set(rnd.nextInt(100)); }
}
for (final Integer i : counter.keySet())
{
final AtomicInteger integer = counter.get(i);
final String format = integer.get() > 1 ? "%d occurs %s times\n" : "%d occurs %s time\n";
System.out.format(format, i, integer);
}
}
}
以下是示例输出:
3 occurs 43 times
98 occurs 16 times
64 occurs 35 times
36 occurs 27 times
37 occurs 19 times
7 occurs 58 times
76 occurs 48 times
77 occurs 40 times
41 occurs 68 times
46 occurs 5 times
13 occurs 100 times
14 occurs 15 times
51 occurs 85 times
17 occurs 40 times
85 occurs 16 times
18 occurs 97 times
25 occurs 10 times
24 occurs 12 times
29 occurs 14 times
91 occurs 2 times
@Override
public void functionToBeRateLimited(@Nonnull final String caller)
{
// do some stuff every time here
super.timesCalled.get(caller).incrementAndGet();
// do some stuff only after a certain time has elapsed since the last time it was done
if (LIMITER.get(caller).tryAcquire())
{
System.out.println(String.format("%s Called Rate Limited Logic up to 2 times a second ( 500 ms )", caller));
}
}
答案 1 :(得分:3)
Jarrod给出了一个很好的答案,但是如果你有某些任务规范,请务必列出它们。
编辑了这个,抱歉这个错误。错过了您在if块中分配了索引。当用户输入0到结束输入时,出现了界外问题。即使用户已完成,您也会尝试插入到数组中。所以放一个if语句来检查0。
int index = 0;
for(int i = 1; i < myList.length - 1; i++){
if(i > 0){
index = input.nextInt();
if (index > 0 && index < myList.length)
myList[index-1]++;
else
break;
}
现在这会起作用,但这不是最好的风格。 for循环不是这里的最佳选择,因为您不知道用户将输入多少个数字。这将解决您的超出范围问题,但您的实施还存在一些其他问题。首先修复此问题,然后处理其余代码。
答案 2 :(得分:3)
package deneme;
import java.util.Scanner;
public class CountOccuranceNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int[] number = new int[100];
System.out.print("Enter the integers between 1 and 100: ");
for (int i = 0; i < number.length; i++) {
int a = input.nextInt();
number[a] += a;
if (a == 0)
break;
}
for (int i = 1; i < number.length; i++) {
if (number[i] != 0) {
if (number[i] / i > 1)
System.out.println(i + " occurs " + number[i] / i + " times");
else
System.out.println(i + " occurs " + number[i] / i + " time"); }
}
}
}
答案 3 :(得分:1)
我认为你的问题就在这一行:
if(myList[index-1] > 1)
System.out.println(index + " occurs " + myList[index-1] + " times ");
如果index为0,您将尝试访问超出范围的数组的-1元素。试试这个:
if(index>0){
if(myList[index-1] > 1)
System.out.println(index + " occurs " + myList[index-1] + " times ");
}
else
System.out.println(index + " occurs " + myList[index-1] + " time ");
}
这使得如果索引为0,则该行不会尝试访问-1元素。
答案 4 :(得分:-1)
public class q7 {
public static void main(String[] args) {
int arr[] = {6, 22, 20, 11, 5, 18, 18, 16, 30, 9, 10, 10, 11, 5, 18, 18, 16};
for (int i = 0; i < arr.length; i++) {
int count = 0;
for (int j = 0; j < arr.length; j++) {
if (arr[i] == arr[j])
count++;
}
System.out.println(arr[i] + "\toccures\t" + count + " times");
}
}
}