0 思維導(dǎo)圖
如圖 1.1。

圖 1.1 控制流思維導(dǎo)圖
1 練習(xí)
對(duì)于所有的練習(xí)題,忽略輸入檢查這一環(huán)節(jié),除非有特別要求。直接假定用戶會(huì)按照給定的格式輸入。
- 練習(xí) 43_1:編寫(xiě)一個(gè)程序讓用戶輸入一個(gè)數(shù)字。這個(gè)數(shù)字需要在 1 到 10 之間。如果用戶輸入了有效的數(shù)字,那么就在控制臺(tái)輸出 "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");
}
- 練習(xí) 43_2:編寫(xiě)一個(gè)程序讀取控制臺(tái)的兩個(gè)數(shù)字然后展示兩個(gè)數(shù)中較大的一個(gè)。
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));
- 練習(xí) 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.編寫(xiě)一個(gè)程序要求用戶輸入一張圖片的寬度和高度,輸出這張圖是橫向的還是縱向的。
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));
- 練習(xí) 43_4:編寫(xiě)一個(gè)超速攝像頭程序。為了簡(jiǎn)單起見(jiàn),忽略關(guān)于攝像機(jī)本身的一些細(xì)節(jié)。編寫(xiě)一個(gè)程序用戶輸入一輛車的速度,如果這個(gè)速度在限速之內(nèi),就在控制臺(tái)輸出 Ok,如果超過(guò)了限速,就需要計(jì)算過(guò)失分。每超出 5km/hr 就要扣掉一分過(guò)失分,如果過(guò)失分超過(guò)12,就需要顯示"License Suspended"。