2015-02-11 52 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; 
} 

}