iT邦幫忙

2025 iThome 鐵人賽

DAY 3
2

根據之前排定的時程,昨天好像有點小超進度(原本要把HelloWorld放在今天來寫的),那麼今天就根據昨天的 HelloWorld 來做延伸吧!
參考的網站:點擊以打開

增加輸入功能✍🏻

根據昨天學到的Console.WriteLine,將後面的文字印出來,寫出第一行 What is your name? 作為問題。
Console.ReadLine()是從主控台讀取使用者輸入的方法,並設一個變數name來存使用者所輸入的內容。
再加上昨天學過的DateTime.Now來存取當前時間。

Console.WriteLine("What is your name?");
var name = Console.ReadLine();
var currentDate = DateTime.Now;
Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!");
Console.Write($"{Environment.NewLine}Press Enter to exit...");
Console.Read();

執行後就能得到以下結果
https://ithelp.ithome.com.tw/upload/images/20250915/20178767CXbPASrgYH.png

變成計算功能🧮

變成讓使用者輸入兩個數字,但因為輸入的為文字形態,所以要將input1以及input2轉成int才能做計算,

Console.WriteLine("Enter 2 number:");
var input1 = Console.ReadLine();
var input2 = Console.ReadLine();
int num1 = int.Parse(input1);
int num2 = int.Parse(input2);
Console.Write($"{Environment.NewLine}Two number total is {num1+num2}");
Console.Write($"{Environment.NewLine}Press Enter to exit...");
Console.Read();

執行後會得到以下結果:
https://ithelp.ithome.com.tw/upload/images/20250915/20178767skvAwWU8Pv.png


上一篇
Day2-開發環境安裝與第一個 C# 專案
下一篇
Day4-認識 C# 的資料型態與變數
系列文
30 天從 Python 轉職場 C# 新手入門6
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言