检查以下条件-
对于奇数和偶数,请检查数字除以2后的余数-
// checking for odd/ even if(n % 2 == 0) { Console.WriteLine("Even"); } else { Console.WriteLine("Odd"); }
对于肯定,否定并检查数字是否为0-
if (n < 0) { Console.WriteLine("Negative Number!"); } else if(n == 0) { Console.WriteLine("Zero"); } else { Console.WriteLine("Positive Number!"); }
以下是完整的代码:
using System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { int n = 19; // checking for odd/ even if (n % 2 == 0) { Console.WriteLine("Even"); } else { Console.WriteLine("Odd"); } // checking for positive, negative or 0 if (n < 0) { Console.WriteLine("Negative Number!"); } else if (n == 0) { Console.WriteLine("Zero"); } else { Console.WriteLine("Positive Number!"); } } }
输出结果
Odd Positive Number!