2015-12-14 30 views
5

我需要使用數組編寫一個程序,該數組需要一個數字並返回該數字中每個數字的事件數。我想我可能在這裏過分複雜。使用數組計算每個數字的位數

import java.util.*; 
class Exercice7 { 

    public static void main(String [] args) { 
    Scanner sc = new Scanner(System.in); 

    System.out.println("Veuillez saisir un nombre naturel:"); // Get number 

    int n = sc.nextInt();          // Store number as n 

    String str = Integer.toString(n);       // Store n as string 

    int length = str.length();         // Store string length as length 

    int arr[] = new int[length];        // Declare array with as many elements as n has digits 

    int digit[] = {0,1,2,3,4,5,6,7,8,9};      // Declare array with the digits to look for 

    int count = 0;            // Number of occurences of each digit 

    for (int i=(length-1); i>=0; i--) {       // Fill array with digits from number input 
     while (n>0) { 
      arr[i]= n%10; 
      n = n/10; 
     } 
    } 

    for (int j=0; j<10; j++) { 
     count = 0; 
     for (int i=0; i<length; i++) { 
      if (arr[i]==digit[j]) { 
       count++; 
      } 
     } 
     if (count>0) { 
     System.out.println(digit[j] + " occurs " + count + " times."); 
     } 
    } 
    } 
} 

此代碼只返回0和1的數量,而且它是錯誤的。有人能把我推向正確的方向嗎?

回答

4

用十個元素聲明數組([0..9]) - 在那裏你會有數字中每個數字的出現。只需使用counts[3]即可獲得數字3的出現次數。

然後,您只需遍歷字符串數字並讀取下一個字符作爲整數並增加計數器。這樣你只有一個循環。例如,您的號碼中有3,則使用counts[3]++

+0

不知道這是你的意思,但它似乎工作! – ktouchie

+0

嗯,我很高興你已經使它的工作 - 這就是爲什麼我沒有提供任何代碼。當你爲(困難的)問題做出自己的回答時,它會更好,更有趣。幹得好,男人,即使我的方式有點不同:) – deem

+0

謝謝,相信!我完全同意。 :) – ktouchie

0

您只有一個計數器。 10位數字需要10個計數器。

可以使用digit陣列櫃:

int digit[] = new int[10]; // all initialized to 0, digit[i] will count the 
          // occurrences of the digit i 

for (int i=0; i<length; i++) { 
    digit[arr[i]]++; 
} 

for (int j=0; j<10; j++) { 
    System.out.println(j + " occurs " + digit[j] + " times."); 
} 
0

你可以嘗試轉換爲字符串並讀取每個字符

int counts[] = {0,0,0,0,0,0,0,0,0,0}; 
int myNumber=123222;//example 
String string=""+myNumber; //converting integer to String 

for(int i=0;i<string.length();i++){ 
try{ 
    int n=Integer.parseInt(string.charAt(i)+"") 
    counts[n]=counts[n]+1; 
}catch(Exception e){} 

} 

然後將其打印出來:

for (int i=0; i<counts.length; i++) { 
    System.out.println(i + " occurs " + counts[i] + " times."); 
} 
+0

counts數組在初始化時應該將所有元素都設置爲0,因爲這些值表示每個數字的計數,而不是數字本身(由索引表示)。 – Ciara

+0

@Ciara你的權利,我修好了,thx – nafas

0

謝謝,deem,爲您的答案。我沒有完全理解你的意思,但它幫助我在正確的軌道上:

import java.util.*; 
class Exercice7 { 

public static void main(String [] args) { 
    Scanner sc = new Scanner(System.in); 

    System.out.println("Veuillez saisir un nombre naturel:"); //* Get number */ 
    int num = sc.nextInt(); //* Store number as n */ 
    String str = Integer.toString(num); //* Store n as string *// 

    char digit[] = {'0','1','2','3','4','5','6','7','8','9'}; 
    int count = 0; 

    for (int i=0; i<10; i++) { 
     for (int j=0; j<(str.length()); j++) { 
      if (str.charAt(j) == digit[i]) { 
       count++; 
      } 
     } 
     if (count>0) { 
      System.out.println(digit[i] + " apparait " + count + " fois."); 
      count = 0; 
     }    
    } 
} 
} 

它可能不是最簡單的方法,但它的工程!如果您想對代碼進行改進,請隨意添加更多評論。