iT邦幫忙

2024 iThome 鐵人賽

DAY 4
1
自我挑戰組

Golang基礎學習與實踐應用系列 第 4

Day 4 變數與運算基礎練習

  • 分享至 

  • xImage
  •  

昨天的練習做個程式解說及補充!
https://ithelp.ithome.com.tw/upload/images/20240913/20169233dFKTN7XTGU.png
https://ithelp.ithome.com.tw/upload/images/20240913/20169233MNlkFLPDbf.png
(1)第4行
var x, y float64
宣告多個變數以「,」格開且需使用相同的資料型態,若資料型態不同,就要分別宣告,範例如下:
https://ithelp.ithome.com.tw/upload/images/20240913/20169233kSklsxN2SN.png
(2)第5行
fmt.Println("請輸入兩個小數:")
這裡可使用fmt.Println或是fmt.Print,差別在於前者會自動換行。使用fmt.Print的範例如下:
https://ithelp.ithome.com.tw/upload/images/20240913/20169233LEVJVDdJTh.png
(3)第6行

fmt.Scanln(&x, &y)

讓使用者可以從鍵盤輸入數據,要記得變數名稱前需要加上「&」位址運算子,把輸入的值給某一變數時,實際上是把資料的值存入變數記憶體位置裡。

算術運算子,用於算數,像是四則運算
加法 +
減法 -
乘法 *
除法 /
取餘數 %

練習算術運算子!
1.計算輸出結果:
a=10, b=20
a=a%5
b=b/6
fmt.printlf(a,b)

當a=10、b=20,計算a%5與b/6?
https://ithelp.ithome.com.tw/upload/images/20240913/20169233KL3N1ZV5cd.png
https://ithelp.ithome.com.tw/upload/images/20240913/20169233Unt4QIbsUO.png

下一篇預告
接下來會學習各式算符!


上一篇
Day 3 變數宣告與輸入輸出
下一篇
Day 5 算符與算術運算實作
系列文
Golang基礎學習與實踐應用18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言