2010-05-02 57 views
0

我試圖從用戶輸入文件中統計2,3和6的倍數。但由於某種原因,我的櫃檯不工作。請任何bosy肝我請。 我的代碼:如何統計輸入文件中數字的倍數?

#include <stdio.h> 
int main (void) 
{ 
    int num[12]; 
    int i; 
    int counttwo; 
    int countthree; 
    int countsix; 
    int total=0; 
    printf("enter 12 integer numbers:\n"); 
    for(i=0;i<12;i++){ 

scanf("%d", &num[i]); 
    } 
    for(i=0;i<12;i++){ 
    counttwo=0; 
     if(num[i]%2==0){ 
     counttwo++; 
     } 
     countthree=0; 
     if(num[i]%3==0) 
    { 
     countthree++; 
    } 
     countsix=0; 
     if(num[i]%6==0) 
     { 
      countsix++; 
} 
     printf("There are %d multiples of 2:\n", counttwo); 
     printf("There are %d multiples of 3:\n", countthree); 
     printf("There are %d multiples of 6:\n", countsix); 
} 
    return 0; 

} 
+5

** 8 **問題和**否**接受答案?通過這個。 – egrunin 2010-05-02 18:51:50

+0

我認爲這需要一個「家庭作業」標籤。你需要學習正確縮進的價值。 – sbi 2010-05-02 18:53:23

+0

你能否回答以前的問題並接受回答你提出的問題的答案?我們在這裏工作的積分! – 2010-05-02 18:54:35

回答

1

您重置計數器變量的每個迭代步驟。把

counttwo=0; 
countthree=0; 
countsix=0; 

之前的代碼for()

1

想想在第二個循環內counttwo,countthreecountsix的值會發生什麼變化。請特別注意行counttwo = 0,countthree = 0countsix = 0

0
  • 復位counttwocountthree,和countsix至0 for-loop之前
  • scanf
  • 移動3 printf的刪除冗餘for-loopfor-loop

的這是固定的代碼

#include <stdio.h> 
int main (void) 
{ 
    int num[12]; 
    int i; 
    int counttwo = 0; //Reset counttwo, countthree, and countsix to 0 
    int countthree = 0; 
    int countsix = 0; 
    int total=0; 
    printf("enter 12 integer numbers:\n"); 

    for(i=0;i<12;i++){ 

     scanf("%d", &num[i]); 

     if(num[i]%2==0){ 
     counttwo++; 
     } 

     if(num[i]%3==0){ 
     countthree++; 
     } 

     if(num[i]%6==0) { 
     countsix++; 
     } 
    } 

    printf("There are %d multiples of 2:\n", counttwo); 
    printf("There are %d multiples of 3:\n", countthree); 
    printf("There are %d multiples of 6:\n", countsix); 

    return 0; 

} 
+0

完美!非常感謝你的小錯誤花了我近一個星期才弄明白。再次感謝! – user292489 2010-05-02 19:32:02

+0

我是能夠得到的倍數的數量,但我也試圖通過在末端添加代碼以打印出的2的多個號碼: 對於(j = 0;Ĵ user292489 2010-05-02 19:55:28