brief introduction
if: first, judge the conditions in brackets. if the conditions are true, execute the procedure in braces. if the conditions are false, skip the contents in braces and execute the following code. (one may not be executed)
Be careful:
(1) if must not be followed by ";, because the program execution error.
(2) if, else: one of the conditions must be executed during execution.
Judge the year of auspice:
Console.WriteLine("Please enter the year");
int year = Convert.ToInt32(Console.ReadLine ());
bool result = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
if (result)
{
Console.WriteLine("Rui");
}
else
{
Console.ReadKey();
}
Login password succeeded:
// Let the user enter the user's password. If the user name is admin, the password is123,The login is successful.
Console.WriteLine("enter one user name");
string username = Console.ReadLine();
Console.WriteLine("Please input a password");
string pwd =Console.ReadLine();
if (username == "admin" && pwd == "123")
{
Console.WriteLine("Login successfully");
}
else
{
Console.WriteLine("Input password or user error");
}
Console.ReadKey();
Judge whether to divide or not:
// User is required to enter two numbersa.b,Ifaand B Divisibility ora+B greater than100,OutputaOtherwise output b Value
Console.WriteLine("Please enter the number one");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please enter the number two");
double b = Convert.ToDouble(Console.ReadLine());
if ((a%b==0 || b%a==0)|| (a+b>100))
{
Console.WriteLine (a);
}
else
{
Console.WriteLine (b);
}
Console.ReadKey ();
Judgment score segment: ABCD
Console.WriteLine("Please enter your score");
double a = Convert.ToDouble(Console.ReadLine());
if (a>=90)
{
Console.WriteLine ("A");
}
else
{
if (a>=80)
{
Console.WriteLine ("B");
}
else
{
if (a >= 70)
{
Console.WriteLine("C");
}
else
{
Console.WriteLine("D");
}
}
}
Console.ReadKey ();
Other: code debugging
- Process by process
- Sentence by sentence
- Line by line (F11)