# 物件導向程式設計實習 [題目](https://hackmd.io/SyXojYMDa),[作業](https://hackmd.io/BJ4nWVVpa) ## 0124 ### 截圖 變數和字串 ![](https://hackmd.io/_uploads/BysxZmCtT.png) 變數使用 ![](https://hackmd.io/_uploads/Hy6ZG7RY6.png) 如果、迴圈 ![](https://hackmd.io/_uploads/B1oyEmCKp.png) 清單(陣列) ![](https://hackmd.io/_uploads/rJro4Q0Fp.png) ### 心得 學習C#是有挑戰性但也挺有趣的過程。C#是物件導向語言,著重於設計和使用類別、物件、繼承和多型(有助於寫出結構化、模組化的程式),有點像C++。基本語法:變數、運算符、條件語句、迴圈等基本概念...又是要記的東西了...。 ## 0221 ### 練習1 ``` Console.Write("請輸入你的名字:"); string n = Console.ReadLine(); Console.WriteLine($"Good Day {n}"); ``` ![image](https://hackmd.io/_uploads/SyBW3Afhp.png) ### 練習2 ``` Console.Write("請輸入名稱:"); string? n = Console.ReadLine(); Console.Write("請輸入提款金額:"); int m = Convert.ToInt32(Console.ReadLine()); Console.WriteLine($"Hi {n}, 提款金額:{m:C0}"); ``` ![image](https://hackmd.io/_uploads/B16k7kXhT.png) ### 練習3 ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _20240221._2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label1.Text = DateTime.Now.ToString(); } private void button1_Click_1(object sender, EventArgs e) { label1.Text = ""; } } } ``` ![image](https://hackmd.io/_uploads/Hkg3iJ736.png) ![image](https://hackmd.io/_uploads/Hym3j1Q2T.png) ## 0306 ### 練習1 ``` using static System.Console; WriteLine("資訊一甲 2 王言淵"); WriteLine(" "); int n1 = 1_2_3; long n2 = 456_789_123L; long max = Int64.MaxValue; long min = Int64.MinValue; int n3 = 0b1011_110; int n4 = 0b_111_100_11; int n5 = 0xFFFF; WriteLine($"Number:{n1:NO},{n2:n0}"); WriteLine($"二進位變十進位:{n3:D5},{n4:D4},{n5:D6}"); WriteLine($".NET Framework型別(num1):{n1.GetType()}"); WriteLine($".NET Framework型別(num2):{n2.GetType()}"); ``` ![image](https://hackmd.io/_uploads/BJkNRKr6T.png) ### 練習2 ``` using static System.Console; float f = 1.2233446F; double d = 1.22334455667789; decimal de = 1.2233445566778899; WriteLine($"float :{f}"); WriteLine($"double :{d}"); WriteLine($"decimal :{de}"); WriteLine($"float:{f}"); ``` ### 練習3 ``` using static System.Console; const float s = 0.579F; Write("請輸入坪數:"); float a = Convert.ToSingle(ReadLine()); WriteLine($"{a}坪 = {s * a}平方公尺"); const float pi = 3.141592653589793238462643383279502884F; const float c = pi * 25.0F; WriteLine($"圓面積={c}"); ``` ![image](https://hackmd.io/_uploads/S167gqBTp.png) ### 練習4 ``` using static System.Console; WriteLine("資訊一甲 2 王言淵"); WriteLine("請輸入你的生日"); string b = ReadLine() WriteLine($"Number:{n1:NO},{n2:n0}"); WriteLine($"二進位變十進位:{n3:D5},{n4:D4},{n5:D6}"); WriteLine($".NET Framework型別(num1):{n1.GetType()}"); WriteLine($".NET Framework型別(num2):{n2.GetType()}"); ``` ## 0313 ### 練習1 ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _0313 { public partial class Form1 : Form { public Form1() {InitializeComponent();} private void textBox1_TextChanged(object sender, EventArgs e) {} private void button1_Click(object sender, EventArgs e) { try { double f,c ; c= Convert.ToDouble(textBox1.Text); f=(c * 9 / 5) + 32; textBox2.Text = Convert.ToString(f); textBox1.Focus(); } catch { textBox2.Text = "輸入錯誤"; textBox1 = null; } } } } ``` ![](https://hackmd.io/_uploads/BkfO89Cpp.png) ### 練習2 ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _0313 { public partial class Form1 : Form { public Form1() {InitializeComponent();} private void button1_Click(object sender, EventArgs e) { try { label4.Text = "提款金額" + textBox2.Text; } catch { } } private void Form1_Load(object sender, EventArgs e) { string name = Microsoft.VisualBasic.Interaction.InputBox("請輸入姓名","輸入"); DialogResult dr = MessageBox.Show(name+"歡迎!", "歡迎", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); Text = name; } } } ``` ## 0320 ### 練習1 ``` using static System.Console; int sum = 0, count = 0; for (; ; ) { Write("請輸入數值做加總:"); int n = Convert.ToInt32(ReadLine()); count += 1;//計數器累計次數 sum += n;//儲存數值 Write("還要繼續嗎?(Y繼續,N離開)"); string? endkey = ReadLine(); if (endkey == "Y") continue; //繼續執行 else if (endkey == "N") { break; } } WriteLine($"輸入{count}個數值,合計:{sum}"); ReadKey(); ``` ![](https://hackmd.io/_uploads/HkhpclOAT.png) ## 0327 ### 練習1 ``` string[] name = ["Molly", "Eric", "Johseph", "Peter", "Iron", "Priyanka"]; int[] age = [24, 26, 24, 26, 28, 25]; Console.WriteLine("---年齡符合24歲---"); for (int i = 0; i < age.Length; i++){ if (age[i] == 24){Console.Write(name[i] + " ");} } Console.WriteLine("\n"); Array.Sort(age); int pos = Array.IndexOf(age, 25); Console.Write($"找到年齡25! 位置:{pos}"); ``` ![image](https://hackmd.io/_uploads/BkeL7ZbJA.png) ### 練習2 ``` Console.WriteLine("Mary Tomas John"); Random rnd = new Random(Guid.NewGuid().GetHashCode()); long[] sum = { 0, 0, 0 }; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { long randomNumber = rnd.Next(40)+60; sum[j] = sum[j] + randomNumber; Console.Write($"{randomNumber} "); } Console.WriteLine(); } Console.WriteLine("---------------------"); Console.Write("Sum: "); for (int i = 0; i < 3; i++) { Console.Write($"{sum[i]} "); } ``` ![image](https://hackmd.io/_uploads/BkyoN--1A.png) ### 練習3 ``` Random r = new Random(Guid.NewGuid().GetHashCode()); int f=r.Next(50) + 10,i=0,j=0,k=0; var save = new int[2, 2, 3]{{ { r.Next(50) + 10, r.Next(50) + 10, r.Next(50) + 10}, { r.Next(50) + 10, r.Next(50) + 10, r.Next(50) + 10}}, { { r.Next(50) + 10, r.Next(50) + 10, r.Next(50) + 10}, { r.Next(50) + 10, r.Next(50) + 10, r.Next(50) + 10} }}; for (k = 0; k < 2; k++) { for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { if (save[k, i, j] == f) { break; }; } } } Console.WriteLine($"元素:{f},位於第{k}個表格,位於第{i}列 第{j}欄\n表格2個,二維表格2*3"); long[] sum = { 0, 0, 0 }; for (k = 0; k < 2; k++) { Console.WriteLine($"-------表格{k + 1}--------"); for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { Console.Write($"{save[k,i,j]} | "); } Console.WriteLine(); } } ``` ![image](https://hackmd.io/_uploads/HyHWRW-JC.png) ## 0410 ### 練習1 ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _0410 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void trackBar2_Scroll(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { PicShow.SizeMode = PictureBoxSizeMode.StretchImage; PicShow.Height = 90; PicShow.Width = 90; // 設定圖片高度和寬度 PicShow.Image = Image.FromFile("C:\\Users\\user\\Pictures\\1.png"); // 載入圖檔 TkbPic.Maximum = 4; TkbPic.Minimum = 1;// 設定TkbPic的最大、最小值 VsbHeight.Maximum = 180; VsbHeight.Minimum = 1; // 設定最大、最小值 VsbHeight.LargeChange = 1; // 設定VsbHeight的快動值 = 1 VsbHeight.Value = PicShow.Height; // VsbHeight的Value值=圖片高度 HsbWidth.Maximum = 180; HsbWidth.Minimum = 1; // 設定最大、最小值 HsbWidth.LargeChange = 1; // 設定HsbWidth的快動值 = 1 HsbWidth.Value = PicShow.Width; // HsbWidth的Value值=圖片寬度 Ttip.SetToolTip(VsbHeight, $"{VsbHeight.Value}"); Ttip.SetToolTip(HsbWidth, $"{HsbWidth.Value}"); Ttip.SetToolTip(TkbPic, "圖片1"); } private void HsbWidth_Scroll(object sender, ScrollEventArgs e) { PicShow.Width = HsbWidth.Value; Ttip.SetToolTip(HsbWidth, $"{HsbWidth.Value}"); } private void VsbHeight_Scroll(object sender, ScrollEventArgs e) { PicShow.Height = VsbHeight.Value; Ttip.SetToolTip(VsbHeight, $"{VsbHeight.Value}"); } private void TkbPic_Scroll(object sender, EventArgs e) { PicShow.Image = Image.FromFile($"C:\\Users\\user\\Pictures\\{TkbPic.Value}.png"); Ttip.SetToolTip(TkbPic, $"圖片{TkbPic.Value}"); } } } ``` ![image](https://hackmd.io/_uploads/S1L9dOXg0.png) :::spoiler 其他截圖 ![image](https://hackmd.io/_uploads/rkKo_u7x0.png) ![image](https://hackmd.io/_uploads/S1YpOOQxC.png) ![image](https://hackmd.io/_uploads/SyIytdmeA.png) ::: ### 練習2 ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _0410 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DtpAlarm.CustomFormat = "yyyy-MM-dd HH:mm"; DtpAlarm.Format = DateTimePickerFormat.Custom; DtpAlarm.MinDate = DateTime.Now.AddMinutes(1); DtpAlarm.MaxDate = DateTime.Now.AddDays(7); } private void button1_Click(object sender, EventArgs e) { timerl.Interval = 1000; timerl.Enabled = true; DtpAlarm.Enabled = false; } private void timer1_Tick(object sender, EventArgs e) { if (DateTime.Now >= DtpAlarm.Value) { timerl.Enabled = false; System.Media.SystemSounds.Beep.Play(); // 發出系統Beep聲 MessageBox.Show(textBox1. Text,"提示"); } } } } ``` ![image](https://hackmd.io/_uploads/BJ8d2OmeR.png) ![image](https://hackmd.io/_uploads/B17TnuQlR.png) ## 0417 ### 練習3 ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _0417 { public partial class Form1 : Form { public Form1() {InitializeComponent();} private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { linkLabel1.Text = "Visual Studio web"; System.Diagnostics.Process.Start("https://visualstudio.microsoft.com/zh-hant/"); } private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("C:\\Users\\user\\Downloads\\20240306w\\20240306w\\bin\\Debug\\20240306w.exe"); } } } ``` ![image](https://hackmd.io/_uploads/SyqBXyalC.png)