Console Read Console Read Line 13 1 static

  • Slides: 48
Download presentation

由鍵盤輸入資料 • Console. Read(); • Console. Read. Line(); 13

由鍵盤輸入資料 • Console. Read(); • Console. Read. Line(); 13

範例1:輸入與輸出 任務)請使用者輸入姓名與年齡。 static void Main(string[] args) { string Your. Name; int age; Console. Write("請輸入你的名字:");

範例1:輸入與輸出 任務)請使用者輸入姓名與年齡。 static void Main(string[] args) { string Your. Name; int age; Console. Write("請輸入你的名字:"); Your. Name = Console. Read. Line(); Console. Write. Line("Hello, {0}", Your. Name); Console. Write("請輸入你的年齡:"); age = Int. Parse(Console. Read. Line()); Console. Write. Line("{0}您好!您是{1}歲", Your. Name, age); Console. Read. Line(); } 29

範例2:檔案讀取 任務)讓使用者輸入一個成績,判斷是否及格。 using System. IO; File. Info file = new File. Info("c: \grade. txt");

範例2:檔案讀取 任務)讓使用者輸入一個成績,判斷是否及格。 using System. IO; File. Info file = new File. Info("c: \grade. txt"); Stream. Reader sr = file. Open. Text(); int score 1, score 2, score 3; double average; score 1 = int. Parse(sr. Read. Line()); score 2 = int. Parse(sr. Read. Line()); score 3 = int. Parse(sr. Read. Line()); average = (score 1 + score 2 + score 3) / 3. 0; Console. Write. Line("平均是{0: f}分", average); Console. Read(); sr. Close(); 30

範例4:重複結構 任務)讀入檔案的10個分數。使用for語法。 using System. IO; File. Info file = new File. Info("c: \grade. txt");

範例4:重複結構 任務)讀入檔案的10個分數。使用for語法。 using System. IO; File. Info file = new File. Info("c: \grade. txt"); Stream. Reader sr = file. Open. Text(); for (int i=1; i<=10; i++) {  Console. Write. Line(sr. Read. Line()); } Console. Read(); sr. Close(); 32

範例5:重複結構 任務)讀入檔案的不定個數的分數。使用while語法。 using System. IO; File. Info file = new File. Info("c: \grade. txt");

範例5:重複結構 任務)讀入檔案的不定個數的分數。使用while語法。 using System. IO; File. Info file = new File. Info("c: \grade. txt"); Stream. Reader sr = file. Open. Text(); while (sr. Peek() > 0)  Console. Write. Line(sr. Read. Line()); } Console. Read(); sr. Close(); 33

範例6:陣列 任務)讀入檔案中的10個分數。 using System. IO; File. Info file = new File. Info("c: \grade. txt");

範例6:陣列 任務)讀入檔案中的10個分數。 using System. IO; File. Info file = new File. Info("c: \grade. txt"); Stream. Reader sr = file. Open. Text(); int[ ] score = new int[10]; for (int i=0; i<10; i++) {  score[i]= int. Parse(sr. Read. Line()); } Console. Read(); sr. Close(); 34

範例7:陣列 任務)讀入檔案中的不定個數的分數。 using System. IO; File. Info file = new File. Info("c: \macdonald. txt");

範例7:陣列 任務)讀入檔案中的不定個數的分數。 using System. IO; File. Info file = new File. Info("c: \macdonald. txt"); Stream. Reader sr = file. Open. Text(); Array. List score = new Array. List(); while (sr. Peek() > 0) { score. Add(sr. Read. Line()); } 35

拖曳「所有Windows Form─Date. Time. Picker」到表單中 設定「Format: Time」、「Show. Up. Down: True」 40

拖曳「所有Windows Form─Date. Time. Picker」到表單中 設定「Format: Time」、「Show. Up. Down: True」 40

事件:調整表單大小 private void Form 1_Resize(object sender, Event. Args e) { if (this. Window. State

事件:調整表單大小 private void Form 1_Resize(object sender, Event. Args e) { if (this. Window. State == Form. Window. State. Minimized) { notify. Icon 1. Balloon. Tip. Text = "哈哈,看不到我了吧"; notify. Icon 1. Show. Balloon. Tip(3000); this. Show. In. Taskbar = false; } // end if } 42

事件:點擊常駐列圖示 private void notify. Icon 1_Double. Click(object sender, Event. Args e) { this. Window.

事件:點擊常駐列圖示 private void notify. Icon 1_Double. Click(object sender, Event. Args e) { this. Window. State = Form. Window. State. Normal; this. Show. In. Taskbar = true; } 43

事件:按下選歌按鈕 private void button 1_Click(object sender, Event. Args e) { Open. File. Dialog my.

事件:按下選歌按鈕 private void button 1_Click(object sender, Event. Args e) { Open. File. Dialog my. FD = new Open. File. Dialog(); my. FD. Filter= "聲音檔(*. mp 3)|*. mp 3"; if (my. FD. Show. Dialog() == Dialog. Result. OK) { text. Box 1. Text = my. FD. File. Name; ax. Windows. Media. Player 1. URL = my. FD. File. Name; ax. Windows. Media. Player 1. Ctlcontrols. stop(); } // end if } 44

事件:按下播放按鈕 private void button 2_Click(object sender, Event. Args e) { ax. Windows. Media. Player

事件:按下播放按鈕 private void button 2_Click(object sender, Event. Args e) { ax. Windows. Media. Player 1. Ctlcontrols. play(); } 45

事件:按下停播按鈕 private void button 3_Click(object sender, Event. Args e) { ax. Windows. Media. Player

事件:按下停播按鈕 private void button 3_Click(object sender, Event. Args e) { ax. Windows. Media. Player 1. Ctlcontrols. stop(); } 46

事件:按下開始按鈕 private void button 4_Click(object sender, Event. Args e) { timer 1. Start(); this.

事件:按下開始按鈕 private void button 4_Click(object sender, Event. Args e) { timer 1. Start(); this. Window. State=Form. Window. State. Minimized; this. Show. In. Taskbar = false; } 47

事件:計時器 private void timer 1_Tick(object sender, Event. Args e) { if ((date. Time. Picker

事件:計時器 private void timer 1_Tick(object sender, Event. Args e) { if ((date. Time. Picker 1. Value. Hour == Date. Time. Now. Hour) && (date. Time. Picker 1. Value. Minute == Date. Time. Now. Minute) && (date. Time. Picker 1. Value. Second == Date. Time. Now. Second)) { if (this. Window. State == Form. Window. State. Minimized) { this. Window. State = Form. Window. State. Normal; this. Show. In. Taskbar = true; } notify. Icon 1. Balloon. Tip. Text = "醒醒!!"; notify. Icon 1. Show. Balloon. Tip(5000); timer 1. Stop(); ax. Windows. Media. Player 1. Ctlcontrols. play(); // Start the music! } 48