### 練習
一、變量和數據類型
- 1. 變量定義與賦值
```cs
using System;
namespace Name
{
? ? class Program
? ? {
? ? ? ? public static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int age = 20;
? ? ? ? ? ? double height = 1.75;
? ? ? ? ? ? string name = "張三";
? ? ? ? ? ? bool isStudent = true;
? ? ? ? ? ? Console.WriteLine($"年齡: {age}, 姓名: {name}, 身高: {height}, 是否學生: {isStudent}");
? ? ? ? }
? ? }
}
```
- 2. 數據類型轉換
```cs
?string numStr = "123";
? ? ? ? ? ? //將numStr轉換為整數類型
? ? ? ? ? ? int num = int.Parse(numStr);
? ? ? ? ? ? int res = num + 456;
? ? ? ? ? ? Console.WriteLine(res.ToString());
```
二、運算符
1. 算術運算符
```cs
int a = 10;
int b = 3;
Console.WriteLine($"a+b={a+b},a-b={a-b},a*b={a*b},a/b={a/b},a%b={a%b}");
```
2. 比較和邏輯運算符
```cs
? ? ? ? ? ? int x = 20;
? ? ? ? ? ? int y = 30;
? ? ? ? ? ? bool res = x == y;
? ? ? ? ? ? bool res1 = x != y;
? ? ? ? ? ? bool res2 = x > y;
? ? ? ? ? ? bool res3 = x < y;
? ? ? ? ? ? bool res4 = x >= y;
? ? ? ? ? ? bool res5 = x <= y;?
? ? ? ? ? ? Console.WriteLine($"{res},{res1},{res2},{res3},{res4},{res5}");
```?
3.?
```cs
? ? bool res = x > 10 && y < 40;
```
三、流程控制
1. 條件語句
```cs
? ? ? ? Console.WriteLine("請輸入分數(0-100)");
? ? ? ? string input = Console.ReadLine();
? ? ? ? int score;
? ? ? ? //int.TryParse(input, out int score):int.TryParse 是一個嘗試將字符串轉換為整數的方法。它有兩個參數:第一個是要轉換的字符串 input,第二個是輸出參數 out int score,用于存儲轉換后的整數值。
? ? ? ? if (!string.IsNullOrEmpty(input) && int.TryParse(input, out score) && score >= 0 && score <= 100)
? ? ? ? // ?空的 string.IsNullOrEmpty() 調用:!string.IsNullOrEmpty() 應該包含一個參數,即 input。
? ? ? ? // ?未賦值的局部變量 score:int.TryParse 應該使用 out score 而不是 out int score,因為 score 已經在前面聲明過了。
? ? ? ? {
? ? ? ? ? ? int GradeLevel = score / 10;
? ? ? ? ? ? string Grade;
? ? ? ? ? ? switch (GradeLevel)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case 10:
? ? ? ? ? ? ? ? case 9:
? ? ? ? ? ? ? ? ? ? Grade = "A";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 8:
? ? ? ? ? ? ? ? ? ? Grade = "B";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 7:
? ? ? ? ? ? ? ? ? ? Grade = "C";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? ? ? Grade = "D";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? Grade = "E";
? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine($"成績等級:{Grade}");
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("輸入字符無效");
? ? ? ? }
```
2. 循環語句
```cs
? ? ? ? ? ? for (int i = 1; i < 11; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ?int res = i*i;
? ? ? ? ? ? ? ?Console.WriteLine(res);
? ? ? ? ? ? }
```
3. 累加和
```cs
? ? ? ? ? ? int i = 1;
? ? ? ? ? ? int sum = 0;
? ? ? ? ? ? while (i<=100)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sum += i;
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? }
? ? ? ? ? ? ?Console.WriteLine(sum);
```
四、方法
1. 方法定義與調用
```cs
? ? public class Program
? ? {
? ? ? ? public int Add(int n,int y)
? ? ? ? {
? ? ? ? ? ? int res = n + y;
? ? ? ? ? ? return res;
? ? ? ? }
? ? ? ? public static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? // ? 創建 Program 類的實例
? ? ? ? ? ? Program pro = new Program();
? ? ? ? ? ? // 通過實例調用非靜態方法 Add
? ? ? ? ? ? int result = pro.Add(6, 1);
? ? ? ? ? ? ?Console.WriteLine(result);
? ? ? ? }
? ? }
```
2. 方法重載
```cs
? ? public class Calculator
? ? {
? ? ? ? // 重載方法:參數順序為 int 和 double
? ? ? ? public double Multiply(int a, double b)
? ? ? ? {
? ? ? ? ? ? return a * b;
? ? ? ? }
? ? ? ? // 重載方法:參數順序為 double 和 int
? ? ? ? public double Multiply(double a, int b)
? ? ? ? {
? ? ? ? ? ? return a * b;
? ? ? ? }
? ? ? ? public static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? // 根據命名class類
? ? ? ? ? ? Calculator calculator = new Calculator();
? ? ? ? ? ? double result1 = calculator.Multiply(5, 3.5);
? ? ? ? ? ? double result2 = calculator.Multiply(5.5, 3);
? ? ? ? ? ? Console.WriteLine("int 和 double 相乘結果: " + result1);
? ? ? ? ? ? Console.WriteLine("double 和 int 相乘結果: " + result2);
? ? ? ? }
? ? }
```
五、字符串操作
1. 字符串拼接
```cs
? ? ? ? string firstName = "張";
? ? ? ? string lastName = "三";
? ? ? ? Console.WriteLine($"{firstName}{lastName}");
```
2. 字符串方法
```cs
? ? ? ? string sentence = "Hello, World!";
? ? ? ? string res1 = sentence.ToUpper();
? ? ? ? string res2 = sentence.Replace("World","C#");
? ? ? ? Console.WriteLine(res1);
? ? ? ? Console.WriteLine(res2);
```
六、綜合練習
1. 簡單的計算器
```cs
? ? public static void Main(string[] args)
? ? {
? ? ? ? Console.WriteLine("請輸入第一個數字:");
? ? ? ? double num1 = Convert.ToDouble(Console.ReadLine());
? ? ? ? Console.WriteLine("請輸入第二個數字:");
? ? ? ? double num2 = Convert.ToDouble(Console.ReadLine());
? ? ? ? Console.WriteLine("請輸入運算符:(+、-、*、/)");
? ? ? ? char op = Console.ReadLine()[0];
? ? ? ? double res = 0;
? ? ? ? switch (op)
? ? ? ? {
? ? ? ? ? ? case '+':
? ? ? ? ? ? ? ? res = num1 + num2;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '-':
? ? ? ? ? ? ? ? res = num1 - num2;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '*':
? ? ? ? ? ? ? ? res = num1 * num2;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '/':
? ? ? ? ? ? ? ? if (num2 != 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? res = num1 / num2;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Console.WriteLine("錯誤: 除數不能為零!");
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? Console.WriteLine("錯誤: 無效的運算符!");
? ? ? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? ?Console.WriteLine("結果: {0} {1} {2} = {3}", num1, op, num2, res);
? ? }
```
2. 猜數字游戲
```cs
using System;
public class Calculator
{
? ? public static void Main(string[] args)
? ? {
? ? ? ? Random ran = new Random();
? ? ? ? int res = ran.Next(1, 3);
? ? ? ? Console.WriteLine("猜猜看,這個數字是多少?");
? ? ? ? while (true)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("請輸入一個數字:");
? ? ? ? ? ? int userGuess;
? ? ? ? ? ? if (int.TryParse(Console.ReadLine(), out userGuess))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (userGuess == res)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Console.WriteLine("恭喜你,猜對了!程序結束。");
? ? ? ? ? ? ? ? ? ? break; // 結束循環
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (userGuess < res)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Console.WriteLine("猜小了,請再試一次!");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Console.WriteLine("猜大了,請再試一次!");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("輸入無效,請輸入一個數字!");
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
```
### 筆記
1. 命名空間和類定義
- 使用 `namespace` 關鍵字定義命名空間,以組織代碼并避免命名沖突。
- 使用 `public class` 關鍵字定義一個公共類。
2. 主方法
- `static void Main(string[] args)` 是程序的入口點,其中 `args` 是一個字符串數組,用于接收命令行參數。
3. 控制臺輸出
- `System.Console.WriteLine` 方法用于向控制臺輸出文本信息。
4. 列表操作
- `List<string>` 是一個泛型列表,用于存儲字符串類型的元素。
- `new List<string>()` 創建一個新的字符串列表實例。
- `Add` 方法用于向列表中添加單個元素。
- `AddRange` 方法用于向列表中添加一個字符串數組作為元素集合。
5. 循環遍歷
- `foreach` 循環用于遍歷列表中的每個元素。
- `var item` 在 `foreach` 循環中隱式類型化為當前元素的類型。
6. 變量聲明
- `var` 關鍵字用于隱式類型化局部變量,編譯器會根據賦值自動推斷變量類型。
7. 數組創建
- `new string[] { "33", "99" }` 創建一個新的字符串數組,并初始化為包含指定字符串元素。