当前位置:网站首页>【愚公系列】2022年7月 Go教学课程 014-运算符之算术运算符
【愚公系列】2022年7月 Go教学课程 014-运算符之算术运算符
2022-07-21 07:07:00 【愚公搬代码】
前言
运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算。例如:2+3,其操作数是2和3,而运算符则是“+”。
在Go语言中运算符大致可以分为5种类型:算术运算符、关系运算符、逻辑运算符、赋值运算符、其他运算符。
一、算术运算符
1.算术运算符的概念
下表列出了所有Go语言的算术运算符。
运算符 | 术语 | 实例 | 结果 |
---|---|---|---|
+ | 相加 | 10 + 20 | 输出结果 30 |
- | 相减 | 10 - 20 | 输出结果 -10 |
* | 相乘 | 10 * 20 | 输出结果 200 |
/ | 相除 | 20 / 10 | 输出结果 2 |
% | 求余 | 20 % 10 | 输出结果 0 |
++ | 自增 | 10++ | 输出结果 11 |
-- | 自减 | 10-- | 输出结果 9 |
相关案例:
package main
import "fmt"
func main() {
var a int = 10
var b int = 20
var c int
c = a + b
fmt.Printf("第一行 - c 的值为 %d\n", c)
c = a - b
fmt.Printf("第二行 - c 的值为 %d\n", c)
c = a * b
fmt.Printf("第三行 - c 的值为 %d\n", c)
c = a / b
fmt.Printf("第四行 - c 的值为 %d\n", c)
c = a % b
fmt.Printf("第五行 - c 的值为 %d\n", c)
a++
fmt.Printf("第六行 - c 的值为 %d\n", a)
a--
fmt.Printf("第七行 - c 的值为 %d\n", a)
}
注意:除数运算除数不能为0、取余运算除数不能为0。
2.案例
2.1 计算圆的面积与周长
package main
import "fmt"
func main() {
//1:确定出n的取值
const PI = 3.14
//2:接收用户输入的半径
fmt.Println("请输入半径:")
var r float64
fmt.Scan(&r)
//3:计算圆的面积
area := PI * r * r
//4:i算圆的周长
p := 2 * PI * r
//5:输出结果
fmt.Printf("面积是%.2f\n", area)
fmt.Printf("周长是%.2f", p)3
}
2.2 计算总分与平均分
package main
import "fmt"
func main() {
//某学生三门课成绩为,语文:90,数学:89,英语:69,编程求总分与平均分。
var chinese int = 90
var math int = 89
var english int = 69
//计算总分
score := chinese + math + english
//平均分
avg := float64(score) / 3
fmt.Printf("总分为%d\n", score)
fmt.Printf("平均分:%.2f", avg)
}
2.3 计算商品价格
package main
import "fmt"
func main() {
/*问题1:某商店T城(T-shirt)的价格为35元/件,裤子(trousers)的价格为120元/条。 小明在该店买了3件T恤和2条裤子,并且打8.8折。请计算并显示小明应该付多少钱? 问题2:如上题中打完8.8折后出现小数,商店为了结算方便, 只收用户整数部分的钱,如本应收用户303.6,现在只收用户303元,应该如何做? */
var shirt int = 35
var trousers int = 120
totalMoney := shirt*3 + trousers*2
realMoney := float64(totalMoney) * 0.88
fmt.Printf("支付:%d", int(realMoney))
}
边栏推荐
- [completion course] development process of IOT / embedded / MCU graduation design project
- Uniapp introduces Tencent map
- int *const p=&i与int const *p=&i有什么区别
- Mysql07(数据更新DML)
- NetworkX的基本用法
- 开源demo| ARCall 小程序开源示例发布
- Oracle sets the maximum number of connections
- Es installation & IK Chinese parser
- Learn the necessary tools of automation selenium think about automated testing in the pit again
- (pytorch进阶之路六)Swin Transformer实现
猜你喜欢
Leetcode [sword finger offer II 068. find the insertion position
[completion course] development process of IOT / embedded / MCU graduation design project
uniapp 使用 u-view 框架小程序的样式问题集合
Appium脚本启动参数如何设置
开源demo| ARCall 小程序开源示例发布
零基础转行软件测试学习要不要报培训班学习,还是自学好?
Cloud Foundry 4:应用程序的生命周期
近红外染料CY7.5标记PNA多肽实验步骤CY7.5-PNA|188Re标记反基因肽核酸(AGPNA)
Niu Ke brushes question 01 - Kiki de duplication of integers and sorting (C language)
铜牛机房项目的优势和劣势
随机推荐
Vs2022 shortcut key settings
奇葩!面试题目竟设置如厕习惯、吃饭时长、入睡时间
Leetcode skimming: related topics of binary tree sequence traversal
2022年化工自动化控制仪表考试试题模拟考试平台操作
45: Chapter 4: developing file services: 6: third party cloud storage solutions [Alibaba cloud OSS]; (purchase OSS services; subscribe to services; create a bucket;)
Interrupt vector table in stm32
NetworkX的基本用法
UnityWebGl项目总结(未完)
Allure测试报告怎么设置
[paper notes] 3D point cloud segmentation pointnet
Selenium被检测为爬虫,怎么屏蔽和绕过
Selenium怎么上传文件,比你想的方式还多
“我放弃编程,写了一本130万字的硬科幻小说”
Word2vec simple summary
How to understand pointers?
Pycharm使用教程:5个非常有用的技巧
Qt:qstring and qstringlist
第一讲 概述
Houdini楼房刚体区域影响破碎笔记
uniapp 引入腾讯地图