C# 實例練習(第二天)


實例練習

1. 完成簡單登錄效果,設置用戶登錄賬號密碼,清空控制台,進入登錄頁面,請求用戶輸入賬號、密碼和驗證碼(隨機產生),並判斷用戶輸入的信息,給出相應的提示。

  C#代碼如下:

  主要知識點:

 (1)//清空控制台 Console.Clear();
 (2)//設置隨機數驗證碼 Random rand = new Random(); int num = rand.Next(1000,10000);

 

 1 //賬戶設置界面  2 /*  3  Console.WriteLine("----------賬戶設置-----------");  4  Console.WriteLine("請設置賬戶:");  5  str1 = Console.ReadLine();  6  Console.WriteLine("請設置密碼:");  7  str2 = Console.ReadLine();  8  //清空控制台  9  Console.Clear(); 10  //用戶登錄界面 11  Console.WriteLine("------------登錄-------------"); 12  Console.WriteLine("請輸入賬戶:"); 13  str3 = Console.ReadLine(); 14  Console.WriteLine("請輸入密碼:"); 15  str4 = Console.ReadLine(); 16  //設置隨機數驗證碼 17  Random rand = new Random(); 18  int num = rand.Next(1000,10000); 19  //輸出驗證碼 20  Console.WriteLine("請輸入驗證碼:"+num); 21  str5 = Console.ReadLine(); 22  int num2=Convert.ToInt32(str5); 23  //進行判斷 24  if(str1==str3&&str2==str4&&num==num2){ 25  Console.WriteLine("您的信息輸入完全正確,可以登陸!"); 26  Console.WriteLine("請按任意鍵繼續……"); 27  } 28  else 29  { 30  Console.WriteLine("對不起,您輸入的信息有誤,請重新輸入,謝謝!"); 31  }

2.  用戶輸入三條邊長,判斷能否組成三角形,並判斷可以組成什么樣的三角形。

代碼如下:

 1 //進行輸出:提示用戶輸入  2 Console.WriteLine("請輸入第一條邊:");  3 //用一個變量接受輸入的值  4 str1 = Console.ReadLine();  5 Console.WriteLine("請輸入第二條邊:");  6 str2 = Console.ReadLine();  7 Console.WriteLine("請輸入第三條邊:");  8 str3 = Console.ReadLine();  9 //轉換為整形 10 int side1, side2, side3; 11 side1 = Convert.ToInt32(str1); 12 side2 = Convert.ToInt32(str2); 13 side3 = Convert.ToInt32(str3); 14 //判斷 15 if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) 16  { 17 if (side1 == side2 || side1 == side3 || side2 == side3) 18  { 19 if (side1 == side2 && side1 == side3) //已經三邊相等,無需另行判斷 20  { 21 Console.WriteLine("可以組成一個等邊三角形"); 22  } 23 else 24  { 25 Console.WriteLine("可以組成一個等邊三角形"); 26  } 27  } 28 else 29  { 30 Console.WriteLine("可以組成一個普通三角形"); 31  } 32 //Console.WriteLine("可以組成一個三角形"); 33  } 34 else 35  { 36 Console.WriteLine("不可以組成一個三角形"); 37  } 38 console.Redkey();

3.  提示用戶輸入兩個數字,並計算這兩個數字之間所有整數的和。

要求:

(1)用戶只能輸入數字
(2)計算兩個數字之間和
(3)要求第一個數字必須比第二個數字小,否則就重新輸入。

答案一:

 1 //while (true)  2 //{  3 // Console.WriteLine("請輸入第1個數字:");  4 // string str1 = Console.ReadLine();  5 // Console.WriteLine("請輸入第2個數字:");  6 // string str2 = Console.ReadLine();  7 // int num11 = 0;  8 // int num22 = 0;  9 // try ----------------------C#異常捕捉機制 10 // { 11 // num11 = Convert.ToInt32(str1); 12 // num22 = Convert.ToInt32(str2); 13 // } 14 // catch (Exception) 15 // { 16 // Console.WriteLine("您輸入的數據類型有誤,請重新輸入!"); 17 // Console.ReadKey(); 18 // return; 19 // } 20 // if (num11 > num22) 21 // { 22 // Console.WriteLine("您輸入的數據大小有誤,請重新輸入!"); 23 // Console.ReadKey(); 24 // Console.Clear(); 25 // continue; 26 // } 27 // int sum = 0; 28 // for (int i = num11; i <= num22; i++) 29 // { 30 // sum += i; 31 // } 32 // Console.WriteLine("{0}和{1}之間所有整數的和為:{2}", num11, num22, sum); 33 // Console.WriteLine("是否退出程序?(Y|N)"); 34 // string flag = Console.ReadLine(); 35 // if (flag == "Y") 36 // { 37 // break; 38 // } 39 // Console.ReadKey(); 40 //}

  答案二:

 1          bool f = true;
 2             while (f) {
 3                 #region 代碼
 4                 Console.WriteLine("請輸入第一個數字");
 5                 string num1 = Console.ReadLine();
 6                 Console.WriteLine("請輸入第二個數字");
 7                 string num2 = Console.ReadLine();
 8 
 9                 int x = 0;//接收  字符串轉換成數字的值
10                 int y = 0;
11 
12                 ///用戶只能輸入數字
13                 try
14                 {
15                     x = Convert.ToInt32(num1);
16                     y = Convert.ToInt32(num2);
17                 }
18                 catch (Exception)
19                 {
20                     Console.WriteLine("您輸入的不是數字");
21                     Console.ReadKey();
22                     return;
23                 }
24 
25                 //求第一個數字必須比第二個數字小
26                 if (x >= y)
27                 {
28                     Console.WriteLine(" 第一個數字比第二個數字大,請重新輸入");
29                     Console.ReadKey();
30                     Console.Clear();//控制台 內容清空
31                     continue;
32                 }
33 
34                 //計算兩個數字之間和
35                 int sum = 0;
36                 for (int i = x; i <= y; i++)
37                 {
38                     sum += i;
39                 }
40 
41 
42                 Console.WriteLine("兩個數之間的和為:{0}",sum);
43 
44                 Console.WriteLine("是否退出程序?(Y|N)");
45                 string flag = Console.ReadLine();
46                 if (flag == "Y") {
47                     break;
48                 }              
49                 #endregion

4.  用方法來實現:請計算出一個整型數組的平均值。

1 //int[] array1 = new int[6]; 2 //double summ = ArrayAvg(array1);//定義變量接收方法的返回值 3 //Console.WriteLine("平均值為:{0}", summ); 4 //Console.ReadKey();//調用方法

方法:

 1 #region 第4題求平均值(方法) 快捷鍵:#region+table鍵  2 /// <summary>  3 /// 4.計算數組的平均值  4 /// </summary>  5 /// 無參有返回值  6 /// <param name="array"></param>  7 static double ArrayAvg(int[] array)  8  {  9 double sum = 0; 10 for (int i = 0; i < array.Length; i++) 11  { 12 array[i] = i; 13 sum += array[i]; 14  } 15 16 return sum; 17  } 18 #endregion

5.   寫一個方法,用來判斷用戶輸入的數字是不是質數 ,再寫一個方法 要求用戶只能輸入數字,如果輸入有誤,就一直讓用戶輸入數字。

 1 //(1)  2 //Console.WriteLine("請輸入一個數字:");  3 //string strr = Console.ReadLine();  4 //JudgmentPrime(strr);  5 //(2)判斷是不是數字  6 //bool boo = true;  7 //while(boo){  8 // Console.WriteLine("請輸入一個數字:");  9 // string str4 = Console.ReadLine(); 10 // bool result = IsNumber(str4); 11 // if (result) 12 // { 13 // boo=false; 14 // break; 15 // } 16 // else{ 17 // Console.WriteLine("您輸的不是一個數字,請重新輸入!"); 18 // Console.ReadKey(); 19 // Console.Clear(); 20 // } 21 22 //} 23         

方法:

 1 /// 5.1判斷質數 2 /// </summary> 3 /// <param name="Prime"></param> 4 static void JudgmentPrime(string Prime) 5 { 6 int num1 = Convert.ToInt32(Prime), sum = 0, k = 1 + num1; 7 for (int i = 1; i <= num1; i++) 8 { 9 if (num1 % i == 0) 10 { 11 sum += i; 12 } 13 } 14 if (sum == k) 15 { 16 Console.WriteLine("您輸入的是一個質數!"); 17 } 18 else 19 { 20 Console.WriteLine("您輸入的不是一個質數!"); 21 } 22 } 23 #endregion

 

 1 /// <summary>  2 /// 5.2判斷輸入值是不是數字  3 /// </summary>  4 /// <param name="str"></param>  5 /// <returns></returns>  6 static bool IsNumber(string str)  7  {  8 bool f = true;  9 int x = 0; 10 try 11  { 12 x = Convert.ToInt32(str); 13  } 14 catch (Exception) 15  { 16 f = false; 17  } 18 return f; 19  } 20 #endregion

6.寫一個方法 計算圓的面積和周長  面積是 pI*R*R  周長是 2*Pi*r

1 //Console.WriteLine("請輸入圓的半徑:"); 2 //string r = Console.ReadLine(); 3 //CircleArea(r);

 

方法:

 1 #region 7.計算圓的周長和面積  2 /// <summary>  3 /// 7.計算圓的周長和面積  4 /// </summary>  5 /// <param name="mianji"></param>  6 /// <param name="zhouChang"></param>  7 static void CircleArea(string banJing)  8  {  9 double num = Convert.ToDouble(banJing); 10 Console.WriteLine("圓的面積為:{0};\n周長為:{1}", Math.PI * num * num, 2 * Math.PI * num); 11  } 12 #endregion

7.  請通過冒泡排序法對整數數組{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }實現升序排序。 

 1 int[] array4 = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };  2 //外循環:控制整體的排序次數  3 for (int i = 0; i < array4.Length - 1; i++)  4  {  5 //內循環:控制每次循環的次數  6 for (int j = i; j < array4.Length - 1; j++)  7  {  8 if (array4[i] > array4[j + 1])  9  { 10 int max = array4[i]; 11 array4[i] = array4[j + 1]; 12 array4[j + 1] = max; 13  } 14 15  } 16  } 17 //遍歷數組 18 foreach (var item in array4) 19  { 20  Console.WriteLine(item); 21  } 22 Console.ReadKey();

8.   用方法來實現:請計算出一個整型數組的平均值:{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求:計算結果如果有小數,則顯示小數點后兩位(四舍五入)。

1 //int[] array5 = { 1, 3, 5, 57, 98, 33, 4, 4, 6, 8, 10 }; 2 //Arrayavg(array5); 3 //Console.ReadKey();

方法:

 1 /// <summary>  2 /// 9.求平均值  3 /// </summary>  4 /// <param name="arrayy"></param>  5 static void Arrayavg(int[] arrayy)  6  {  7 double sum = 0;//轉換為double類型,否則不會輸出小數  8 for (int i = 0; i < arrayy.Length; i++)  9  { 10 sum += arrayy[i]; 11  } 12 double aa = sum / arrayy.Length;//同上 13 double kk = Math.Round(aa, 2);//同上 14 Console.WriteLine("平均值為:{0}", kk); 15  } 16 #endregion

本題知識點補充:

C#中:   

  • 整數除以整數,結果仍為整數;
  • 整數除以小數,結果為小數;
  • 小數除以整數,結果也為小數。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM