2014-03-26 50 views
0

如何輸入一個3的倍數的數字,它顯示if語句的數字不是3的倍數,它會轉到else語句。如果你能幫助我,我將非常感激。Multi of 3 If else語句

 Console.Write("Enter Number: "); 
     int N = Convert.ToInt32(Console.ReadLine()); 
     if (N == 3) //<<< what do I do here 
     { 
      Console.WriteLine("Is multi of 3"); 
     } 
     else 
     { 
      Console.WriteLine("Is not multi of 3"); 
     } 
     Console.ReadLine(); 
+1

N%3 == 0 http://www.cprogramming.com/tutorial/modulus.html – kenny

回答

3

這很簡單:

if (N % 3 == 0) 
    { 
     Console.WriteLine("Is multi of 3"); 
    } 

所以要用:

Console.Write("Enter Number: "); 
int N = Convert.ToInt32(Console.ReadLine()); 
if (N % 3 == 0) 
{ 
    Console.WriteLine("Is multi of 3"); 
} 
else 
{ 
    Console.WriteLine("Is not multi of 3"); 
} 
    Console.ReadLine(); 
+0

哦,謝謝,我不停由於某種原因,認爲N/3大聲笑 – TheBoringGuy

6

使用模運算來代替:

if (N % 3 == 0) 

它做了分工後返回餘數。

如果除以3的餘數爲0,則知道您有3的倍數。