2017-08-09 82 views
0

我剛剛開始學習C#,但我無法弄清楚爲什麼這個示例問題是以這種方式進行佈局的。也許我不瞭解操作順序如何工作的基礎知識。有人可以解釋這個程序的「while」部分是如何工作的嗎?我不明白當這行後沒有引用num時,「num = num/10」是什麼。它會影響前一行嗎?感謝您的任何見解。C#程序獲取數字並顯示數字總和

/* 
* C# Program to Get a Number and Display the Sum of the Digits 
*/ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Program 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     int num, sum = 0, r; 
     Console.WriteLine("Enter a Number : "); 
     num = int.Parse(Console.ReadLine()); 
     while (num != 0) 
     { 
      r = num % 10; 
      num = num/10; 
      sum = sum + r; 
     } 
     Console.WriteLine("Sum of Digits of the Number : "+sum); 
     Console.ReadLine(); 

    } 
} 
} 
+0

num是除法後引用的,它只是不在同一個循環迭代中。它用於下一個循環迭代(從條件開始評估)。它不會影響當前循環迭代中的前一行,但它會影響下一次迭代。 – Smit

回答

3

是否影響前行?

如果以前您的意思是while condition,那麼是的,只是通過循環中的下一個檢查來影響檢查。

只是做所有的數字編號

的總結一樣,如果你輸入的是

1235 

那麼輸出將是

1+2+3+5 = 11 

while循環將永遠被執行的代碼如果條件爲真

num != 0 

而在循環中,您只需修改num,以便在下一個條件檢查中再次與0進行比較。

如果它總是正確的,即總是

num != 0 

然後你會得到一個無限循環,這可能在一定條件下。

歡迎大家來編碼世界:d

0

我想你會在代碼中的無限循環告終。使用while循環時,只要num變量具有值,就會始終執行while循環內的代碼。

而在您的while循環代碼中,您的sum值與公式沒有關係。

r = num % 10; 
num = num/10; 
sum = sum + r; 

相反,它改變這個地方sum設置爲num/10

r = num % 10; 
sum = num/10; 
sum = sum + r; 

價值和使用if語句。因爲每次用戶輸入號碼都保證只執行一次。所以不需要while循環。

0
class Program 
    { 
     static void Main(string[] args) 
     { 
      int num, sum = 0, r; 
      Console.WriteLine("Enter a Number : "); 
      num = int.Parse(Console.ReadLine()); 
      while (num != 0) 
      { 
       /* Important: The logic behind is to add the numbers from the last one 
       * to the first one. 
       * 
       * For example, 123 will be added in this way: 3 + 2 + 1 
       * We will use number 123 as example.*/ 

       /* Comment: 
       * 
       * 1st run: 123 % 10, remainder will be 3. Anything modulus 10 will 
       * always get the final digit. Now, we have r = 3, which will be used 
       * in the final sum below. 
       * 
       * 2nd run: 12 % 10, remainder will be 2. Now, we have r = 2, which 
       * will be used in the final sum below. 
       * 
       * 3rd run: 1 % 10, remainder will be 1. Now, we have r = 1, which will 
       * be used in the final sum below. 
       */ 
       r = num % 10; 

       /* Comment: 
       * 
       * 1st run: 123/10, answer will be 12. If you are wondering why it 
       * isn't 12.3, then it is because the datatype in used is "int", so the 
       * answer will always be a round number, so 12.3 in round number will 
       * be 12, which is the 2 numbers that have not been added, so now, 
       * num = 12, and we have managed to discard 3 because we no longer need 
       * it because it will be added in the final sum below. 
       * 
       * 2nd run: 12/10, again answer is not 1.2, but num = 1. 
       * We have managed to discard 2 because we no longer need it because it 
       * will be added in the final sum below. 
       * 
       * 3rd run: 1/10, again answer is not 0.1, but num = 0. 
       * We have managed to discard 1 because we no longer need it because it 
       * will be added in the final sum below. This will be the final run. 
       */ 
       num = num/10; 

       /* Comment: 
       * 1st run: 0 + 3 (1st run remainder) 
       * 2nd run: 3 + 2 (2nd run remainder) 
       * 3rd run: 5 + 1 (3rd run remainder) 
       */ 
       sum = sum + r; 
      } 
      Console.WriteLine("Sum of Digits of the Number : " + sum); 
      Console.ReadLine(); 
     } 
    } 

是的,「num」變量被「while」循環用來檢查當前的情況。如果「num」不是「0」,那麼邏輯將繼續運行。希望這可以幫助。

1
Example We Input a Number "123" 

When it goes to the condition While Number(123) != (is not equal to) 0 Then it will perform the code. 
First get the remainder by using modulus %. 
Second get the Whole number by using division. 
Third Get the sum. 

1.) r = num % 10 | The value of r now is 3 
2.) num = num/10 | The value of num now is 12. 
3.) sum = sum + r | The value of sum here is 0 + the remainder 3. 

It will go the the while statement again. Is the number(12) != (is not equal to) 0 then it will perform the code. 

Take note the value now of Num is 12 ok. 
Perform again the code. 

1.) r = num % 10 | The value of r now is 2 
2.) num = num/10 | The value of num now is 1. 
3.) sum = sum + r | The value of sum here now is 3 + the remainder now is 2. 

So the sum now is 5. Then it will loop again because Num now is equal to 1. Then 
Perform the code again. 

I think this explains the best for you.