The user enters a grade of a student.
You will check then whether the entered number is in the set of possible values 1, 2, 3, 4, or 5.
The if condition can be used to enumerate the individual alternatives.
using System; class Program/*from ww w.java2s . c o m*/ { static void Main(string[] args) { // Input Console.Write("Enter a grade: "); string input = Console.ReadLine(); int grade = Convert.ToInt32(input); // Evaluating if (grade == 1 || grade == 2 || grade == 3 || grade == 4 || grade == 5) { Console.WriteLine("Input OK."); } else { Console.WriteLine("Incorrect input."); } } }
The following code solves the exercise using a range check.
using System; class Program// w w w . j a va 2 s. co m { static void Main(string[] args) { // Input Console.Write("Enter a grade: "); string input = Console.ReadLine(); int grade = Convert.ToInt32(input); // Evaluating if (grade >= 1 && grade <= 5) { Console.WriteLine("Input OK."); } else { Console.WriteLine("Incorrect input."); } } }