Knowledge points-Statements

Source: Internet
Author: User

Data type-variable constant-operator (expression)-statement (order, branch, loop)-array-function

Supplement: The use of% (remainder)
Determine whether a number can be divisible, and if it can be divisible, the remainder is 0; classification

Case

static void Main111 (string[] args)
{
Enter a two-digit number from the keyboard to determine if it is related to 7
int A;
BOOL Ok1=true,ok2=true,ok3=true,ok;
Input
A = Convert.ToInt32 (Console.ReadLine ());
Operation
1. Divisible by 7
Ok1 = a% 7 = = 0;
2. Single digit is 7
Ok2 = a% 10 = = 7;
3.10-digit is 7
OK3 = A/10 = = 7;

OK = Ok1 | | Ok2 | | OK3;

Output
String Jieguo = (ok = = true)? "Related to 7": "Unrelated to 7");
Console.WriteLine (Jieguo);
}

Branch statements

1.if

if (an expression)
{
Statement
}

Attention:
1. If the expression is true, the curly braces can be omitted if only one statement is executed. If the expression is set, the number of statements that need to be executed is greater than or equal to 2, and these phrases must be placed in curly braces.
Do not add a semicolon after the 2.if parenthesis.

2.if...else ...
if (an expression)
{
}
Else
{
}

Note: the same if
1. There can be if there is no else, but there is else, it must be preceded by an IF.
There are no parentheses and conditional expressions after 2.else.
3. If the brace is satisfied after the conditional walk, ignore the else directly

Case 1

Enter three numbers, three numbers to find the largest one.

static void Main (string[] args)
{
int A, b, C;
int Zuida;
A = Convert.ToInt32 (Console.ReadLine ());
b = Convert.ToInt32 (Console.ReadLine ());
c = Convert.ToInt32 (Console.ReadLine ());

if (a > B)
{
Zuida = A;
}
Else
{
Zuida = b;
}


if (Zuida < c)
{
Zuida = C;
}

Console.WriteLine (Zuida);
}

Case 2

Determine whether the year entered is a leap years or a common year

static void Main333 (string[] args)
{
Console.Write ("Please enter a year:");
int year = Convert.ToInt32 (Console.ReadLine ());

1. Divisible by 400, 2. Divisible by 4, but not divisible by 100
if (year%400==0 | | (year%4==0 && year%100!=0))
{
Console.WriteLine ("Leap Year");
}
Else
{
Console.WriteLine ("Common Year");
}

3. Multi-Branch
If ... else if ... else if.....else
if (expression 1)
{
Statement 1;
}
else if (expression 2)
{
Statement 2;
}
...
Else
{
Statement N;
}

Case 1:

static void Main (string[] args)
{
Console.Write ("Please enter the Year:");
int year = Convert.ToInt32 (Console.ReadLine ());
Console.Write ("Please enter the month:");
int month = Convert.ToInt32 (Console.ReadLine ());

if (month = = 1 | | month = = 3 | | month = = 5 | | month = = 7 | | month = 8 | | month = = | | month = = 12)
{
Console.WriteLine ("This month is 31 days");
}
else if (month = = 4 | | month = = 6 | | month = = 9 | | month = = 11)
{
Console.WriteLine ("This month is 30 days");
}
else if (month = = 2)
{
If (year% 400 = = 0 | | (year% 4 = = 0 && Year% 100! = 0))
{


Console.WriteLine ("29 Days");
}
Else
{
Console.WriteLine ("28 Days");
}
}
Else
{
Console.WriteLine ("Problem of the Month");
}
}

Case 2

static void Main (string[] args)
{
Console.WriteLine ("Please enter Age:");
int age = Convert.ToInt32 (Console.ReadLine ());

if (age > 0 && Age < 6)
{
Console.WriteLine ("Childhood");
}
else if (age >= 6 && Age < 16)
{
Console.WriteLine ("Juvenile");
}
else if (age >= && Age < 30)
{
Console.WriteLine ("Youth");
}
else if (age >=-&& Age < 60)
{
Console.WriteLine ("Middle Age");
}
else if (age > && Age < 130)
{
Console.WriteLine ("Old Age");
}
Else
{
Console.WriteLine ("Incorrect input");
}
}

4.if nesting

if (an expression)

{
if (an expression)
{
}
}
Else
{
if (an expression)
{
}
Else
{
}
}

Case 1

Do a little game with the computer scissors. 0-Scissors, 1-stone, 2-cloth
Require output 0,1,2, the computer generates random numbers, compared with human input than the judge who wins.

Computer generated random number:
Random rand = new Random ();
int c = Rand. Next (3);

static void Main (string[] args)
{
A computer punch.
Random rand = new Random ();
int c = Rand. Next (3);
Man Out Fist
Console.Write ("Please punch out");
int r = Convert.ToInt32 (Console.ReadLine ());

if (c = = 0)
{
if (r = = 0)
{
Console.WriteLine ("Computer-to-person leveling");
}
else if (r = = 1)
{
Console.WriteLine ("Man wins computer");
}
else if (r = = 2)
{
Console.WriteLine ("Computer Winner");
}
Else
{
Console.WriteLine ("Punch Error");
}
}
else if (c = = 1)
{
if (r = = 0)
{
Console.WriteLine ("Computer Winner");
}
else if (r = = 1)
{
Console.WriteLine ("Computer-to-person leveling");
}
else if (r = = 2)
{
Console.WriteLine ("Man wins computer");
}
Else
{
Console.WriteLine ("Punch Error");
}
}
else if (c = = 2)
{
if (r = = 0)
{
Console.WriteLine ("Man wins computer");
}
else if (r = = 1)
{
Console.WriteLine ("Computer Winner");
}
else if (r = = 2)
{
Console.WriteLine ("Computer-to-person leveling");
}
Else
{
Console.WriteLine ("Punch Error");
}
}
}

Case 2

Do a little game of fate:
Enter the name of the man, the woman's name, Output Fate index, give advice.

static void Main (string[] args)
{
Console.Write ("The Man's Name:");
String nan = Console.ReadLine ();
Console.Write ("Woman's Name:");
String NV = Console.ReadLine ();

Random rand = new Random ();
int n = rand. Next (100);
n++;

String Jianyi = "";
if (n > 0 && N < 30)
{
Jianyi = "Break Up";
}
else if (n >= && n < 60)
{
Jianyi = "work together";
}
else if (n >= && n <= 80)
{
Jianyi = "happy couple";
}
Else
{
Jianyi = "Mandarin duck with";
}

Console.WriteLine (The Fate Index for "{0} and {1}" is: {2}. Recommendation: {3} ", Nan, NV, N,jianyi);
}

Case 3

Axx+bx+c==0 (a!=0). Input a,b,c give this unary two-time equation, show the number of root?

static void Main (string[] asdfasdf)
{
Console.WriteLine ("Unary two times equation a*x*x+b*x+c = = 0, please enter the value of A,b,c");
int a = Convert.ToInt32 (Console.ReadLine ());
int b = Convert.ToInt32 (Console.ReadLine ());
int c = Convert.ToInt32 (Console.ReadLine ());

if (a = = 0)
{
Console.WriteLine ("Not a two-dimensional equation");
}
Else
{
int delta = b * b-4 * a * C;
if (Delta > 0)
{
Console.WriteLine ("Two Unequal real roots");
}
else if (delta = = 0)
{
Console.WriteLine ("Two Equal real Roots");
}
Else
{
Console.WriteLine ("No real Roots");
}
}
}

Knowledge points-Statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.