2015-02-11 56 views
0

我想显示一个整数在一个数组中发生了多少次,但我得到一个无限循环/逻辑错误。例如,如果用户输入: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 

任何帮助将真正理解。 注意:这不是一个任务或作业,这是一个从Y.D的Java书籍的介绍开始的练习题。郎

import java.util.*; 

public class CountNumbers { 

    public static void main(String[] args) { 
     System.out.println("Enter the integers between 1 and 100: "); 
     int[] arrayRefVar = createList(); 
     int[] countNum = countNumbers(arrayRefVar); 
     displayCount(countNum, arrayRefVar); 
    } 

    public static int[] createList() { 
     Scanner Input = new Scanner(System.in); 
     int[] List = new int[100]; 
     int i = 0; 

     while (List[i] != 0) { 
      List[i] = Input.nextInt(); 
      i++; 
     } 
     return List; 
    } 

    public static int[] countNumbers(int[] List) { 
     int[] count = new int[100]; 

     for (int i = 0; i < count.length; i++) { 
      count[i] = i; 
     } 

     int[] countNum = new int[List.length]; 

     for (int i = 0; i < countNum.length; i++) { 
      countNum[i] = 0; 
     } 

     for (int i = 0; i < List.length; i++) { 
      for (int j = 0; j < count.length; j++) { 
       if (List[i] == count[j]) { 
        countNum[i]++; 
       } 
      } 
     } 
     return countNum; 
    } 

    public static void displayCount(int[] countList, int[] arrayRefVar) { 
     for (int i = 0; i < arrayRefVar.length; i++) { 
      System.out.println(arrayRefVar[i] + " occurs " + countList[i] + " " + checkPlural(arrayRefVar[i])); 
     } 
    } 

    public static String checkPlural(int n) { 
     if (n > 1) { 
      return "times"; 
     } else { 
      return "time"; 
     } 
    } 
} 
+0

如果你要导入的java.util。*,你可能会避免使用'List'作为变量。这不会很好。 – 2015-02-11 21:51:50

回答

0

如果您的输入应该以0结尾,那么您应该检查当前读取的int是否为零。

while(i < List.length) { 
    List[i] = Input.nextInt(); 
    if(List[i] == 0) 
     break ; 
    ++i; 
} 

既然你是递增i后检查的条件,你没有检查当前值。

注:nextInt()Scanner类方法可以抛出异常,即:InputMismatchExceptionNoSuchElementExceptionIllegalStateException。所以要么在try catch块中处理它,要么通过抛出异常来处理它。

-1

一个问题是,用户输入您的while循环从来没有进入。您使用0作为标记值来退出用户输入,但是,当初始化整数数组时,默认情况下它们全部为0。

int[] List = new int[100]; 

int i = 0; 
//problem: while loop never entered 
while (List[i] != 0) { 
+0

不正确,请看下面的行。他正在改变阵列内容。 – Crazyjavahacking 2015-02-11 21:52:51

+0

@Crazyjavahacking你能更具体吗?我从他引用的代码将永远不会要求用户输入。他初始化了一个整数数组,然后立即检查条件(List [0]!= 0)。它永远不会是真的,除非他在循环之前首先要求输入... – 2015-02-12 16:51:08

0

所以我终于在无数尝试后得到它,如果任何人有任何建议,使代码更有效的输入将不胜感激。这里是:

import java.util.Random;

公共类CountSingleDigits {

public static void main (String[] args) { 

    int[] arrayRefVar = createList(); 

    int[] counts = new int[10]; 

    for (int i = 0; i < 10; i++) { 

     counts[i] = i; 
    } 

    int[] tempCounts = new int[10]; 

    for (int i = 0; i < arrayRefVar.length; i++) { 

     for (int j = 0; j < 10; j++) { 

      if (arrayRefVar[i] == counts[j]) { 

       tempCounts[j]++;  
      } 
     } 
    } 

    for (int i = 0; i < 10; i++) { 

     System.out.println(counts[i] + " appears " + tempCounts[i] + " times "); 
    } 

    for (int i = 0; i < arrayRefVar.length; i++) { 

     if (i % 10 == 0) { 

      System.out.println(); 
     } 

     System.out.print(arrayRefVar[i] + " "); 
    } 
} 

public static int[] createList() { 

    Random f = new Random(); 

    int[] List = new int[100]; 

    for (int i = 0; i < List.length; i++) { 

     List[i] = f.nextInt(9); 
    } 

    return List; 
} 

}