0 思维导图
如图 1.1。
图 1.1 控制流思维导图
1 练习
对于所有的练习题,忽略输入检查这一环节,除非有特别要求。直接假定用户会按照给定的格式输入。
- 练习 43_1:编写一个程序让用户输入一个数字。这个数字需要在 1 到 10 之间。如果用户输入了有效的数字,那么就在控制台输出 "Valid"。否则输出 "Invalid"。
int input;
Console.WriteLine("Please enter a number between 1 to 10:");
input = Convert.ToInt32(Console.ReadLine());
if (input > 0 && input < 11)
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
- 练习 43_2:编写一个程序读取控制台的两个数字然后展示两个数中较大的一个。
int number1;
int number2;
Console.WriteLine("Please enter two numbers:");
number1 = Convert.ToInt32(Console.ReadLine());
number2 = Convert.ToInt32(Console.ReadLine());
var result = number1 > number2 ? number1 : number2;
Console.WriteLine(string.Format("The bigger one is {0}", result));
- 练习 43_3:Write a program and ask the user to enter the width and height of an image. Then tell if the image is landscape or portrait.编写一个程序要求用户输入一张图片的宽度和高度,输出这张图是横向的还是纵向的。
int width;
int height;
Console.WriteLine("Please enter the width and height of a picture:");
width = Convert.ToInt32(Console.ReadLine());
height = Convert.ToInt32(Console.ReadLine());
var result = width > height ? "landscape" : "portrait";
Console.WriteLine(string.Format("The image is {0}", result));
- 练习 43_4:编写一个超速摄像头程序。为了简单起见,忽略关于摄像机本身的一些细节。编写一个程序用户输入一辆车的速度,如果这个速度在限速之内,就在控制台输出 Ok,如果超过了限速,就需要计算过失分。每超出 5km/hr 就要扣掉一分过失分,如果过失分超过12,就需要显示"License Suspended"。