2017-08-05 31 views
0

以下代碼不會輸出所需的結果。它只是通過n迭代,而不是y。我想我正在犯一些愚蠢的錯誤。有人可以解釋爲什麼以及如何解決它?爲什麼下面的代碼不通過m循環?

int product(int m,int n){ 
     int x,p; 
     while(m>=100){ 
      while(n>=100){ 
       printf("Value of m= %d and n=%d\n",m,n); 
       x=m*n; 
       p=palindrome(x); 
       if (p==0) 
       { 
        printf("These two numbers are %d and %d\n",m,n); 

       } 
       n--; 
      } 
      m--; 

     } 
     return 0; 
    } 

完整代碼:http://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMbXZ6M3Y4UVlMSHM

+0

它看起來像你通過內部循環,最終會失敗的整個條件N> = 100遞減第一次,假設n大於從100開始。 – Matt

+0

「這是纔剛剛到n迭代,而不是Y」。什麼?一個變量? –

回答

4

由於`n」爲沒有任何更多> = 100。因此,外部循環迭代但第一個之後從未進入內部。

解決辦法:小心!!!! - 如果你執行它網上你會殺了你的瀏覽器:)

int product(int m,int n){ 
    int x,p, savedn = n; 
    while(m>=100){ 
     while(n>=100){ 
      printf("Value of m= %d and n=%d\n",m,n); 
      x=m*n; 
      p=palindrome(x); 
      if (p==0) 
      { 
       printf("These two numbers are %d and %d\n",m,n); 

      } 
      n--; 
     } 
     m--; 
     n = savedn; 

    } 
    return 0; 
} 
+0

謝謝,現在我意識到我的錯誤。 –

相關問題