C # example: write a program to test the standard height and weight, input the date year month day, calculate the day of the year (considering the leap year ),

Source: Internet
Author: User

C # example: write a program to test the standard height and weight, input the date year month day, calculate the day of the year (considering the leap year ),

I. switchStatement exercise example

// Compile a program to test the standard height and weight.

// Calculation formula:

// Men's standard weight (kg) = height (cm)-105;

// Women's standard weight (kg) = height (cm)-100;

// Project requirements:

// Start the program, and the text prompts you to select a gender.

// (1) male (2) Female

// Text prompt, enter the height and weight.

// Output the standard weight after user input

// Give a comment:

// Actual weight/standard weight-Proportion

// Severe malnutrition with a standard weight of less than 60%

// The standard weight is 60% ~ 80% moderate malnutrition

// Weight 80% ~ 90% mild malnutrition

// The standard weight is 90% ~ 110% is normal

// The standard weight is 110% ~ 120% Mild Obesity

// Standard weight: 120% obesity

Console. WriteLine ("select gender 1: Male \ t2: female ");

Intgender = int. Parse (Console. ReadLine ());

Console. WriteLine ("Enter height ");

Intheight = int. Parse (Console. ReadLine ());

Console. WriteLine ("Enter the weight ");

Intweight = int. Parse (Console. ReadLine ());

IntstanderWeight = 0;

Switch (gender)

{

Case1:

StanderWeight = height-105;

Break;

Case2:

StanderWeight = height-100;

Break;

Default:

Console. WriteLine ("this gender does not exist ");

Break;

}

Floatrate = weight * 1.0f/standerWeight; // * 1.0f, which is converted into a decimal number and must be added with f

If (rate <0.6f) // f can be added or not

{

Console. WriteLine ("severe malnutrition ");

}

Elseif (rate <0.8f) // otherwise, it includes rate> = 0.6 & rate <0.8

{

Console. WriteLine ("moderate malnutrition ");

}

Elseif (rate <0.9)

{

Console. WriteLine ("mild malnutrition ");

}

Elseif (rate <1.1)

{

Console. WriteLine ("normal ");

}

Elseif (rate <1.2)

{

Console. WriteLine ("Mild Obesity ");

}

Else

{

Console. WriteLine ("Obese ");

}

Ii. goto jump statement

Inttimes = 0;

Times ++;

Console. WriteLine ("hello world" + times );

Gotolabel;

Example 1:

// Enter a date, year, month, and day to calculate the day of the year (considering the leap year)

1. // method 2

Console. WriteLine ("Enter year ");

Intyear = int. Parse (Console. ReadLine ());

Console. WriteLine ("Enter month ");

Intmonth = int. Parse (Console. ReadLine ());

Console. WriteLine ("input date ");

Intday = int. Parse (Console. ReadLine ());

// Record the accumulated days

InttotalDays = 0;

Switch (month)

{

Case12:

TotalDays + = 30;

Gotocase11;

Case11:

TotalDays + = 31;

Gotocase10;

Case10:

TotalDays + = 30;

Gotocase9;

Case9:

TotalDays + = 31;

Gotocase8;

Case8:

TotalDays + = 31;

Gotocase7;

Case7:

TotalDays + = 30;

Gotocase6;

Case6:

TotalDays + = 31;

Gotocase5;

Case5:

TotalDays + = 30;

Gotocase4;

Case4:

TotalDays + = 31;

Gotocase3;

Case3:

TotalDays + = 28; // determines whether it is a leap year;

If (year % 4 = 0 & amp; year % 100! = 0) | year % 400 = 0)

{

TotalDays ++;

}

Gotocase2;

Case2: /// add the number of days in January to the number of days in case1, that is, the number of days.

TotalDays + = 31;

Gotocase1;

Case1: // It must be written in reverse mode because break exists;

TotalDays + = day;

Break;

Default:

Break;

}

Console. WriteLine ("{0}-{1}-{2}-{3} day of this year", year, month, day, totalDays );

2. Method 1: implemented using the if else statement. (The above program functions)

Console. WriteLine ("Enter the year ");

Intyear = int. Parse (Console. ReadLine ());

Console. WriteLine ("Enter the month ");

Intmonth = int. Parse (Console. ReadLine ());

Console. WriteLine ("Enter the date ");

Intday = int. Parse (Console. ReadLine ());

// Record the accumulated days

// 2 20

InttotalDays = 0;

TotalDays + = day;

Month-= 1;

If (month = 11)

{

TotalDays + = 30;

Month-= 1;

}

If (month = 10)

{

TotalDays + = 31;

Month-= 1;

}

If (month = 9)

{

TotalDays + = 30;

Month-= 1;

}

If (month = 8)

{

TotalDays + = 31;

Month-= 1;

}

If (month = 7)

{

TotalDays + = 31;

Month-= 1;

}

If (month = 6)

{

TotalDays + = 30;

Month-= 1;

}

If (month = 5)

{

TotalDays + = 31;

Month-= 1;

}

If (month = 4)

{

TotalDays + = 30;

Month-= 1;

}

If (month = 3)

{

TotalDays + = 31;

Month-= 1;

}

// The if condition that can be executed below must be greater than that in January

If (month = 2)

{

TotalDays + = 28;

Month-= 1;

// Determine whether a leap year is a leap year plus 1

If (year % 4 = 0 & amp; year % 100! = 0 | year % 400 = 0)

{

TotalDays ++;

}

}

If (month = 1)

{

TotalDays + = 31;

Month-= 1;

}

Console. WriteLine ("this is the day {0} of this year", totalDays );

III,Switch (day)

{

Case 1: // if the conditions are the same, the last one can be executed,

Case 2:

Case 3:

Case 4:

Case 5:

Console. WriteLine ("Today's workday ");

IV,// Score A, B, C, D, and E

Console. WriteLine ("Enter your score ");

Intscore = int. Parse (Console. ReadLine ());

Charlevel = 'a ';

Switch (score/10)

{

Case10:

Case9:

Level = 'a ';

Break;

Case8:

Level = 'B ';

Break;

Case7:

Level = 'C ';

Break;

Case6:

Level = 'D ';

Break;

Default:

Level = 'E ';

Break;

}

Console. WriteLine (level );

Related Article

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.