学生信息展示
题目描述
定义
Student结构体:姓名、年龄、成绩。实现方法
Show()打印完整信息。
输出示例
姓名:小明 年龄:18 成绩:92.5
实现代码
Go
package main
import "fmt"
type Student struct {
Name string
Age int
Score float64
}
func (s Student) Show() {
fmt.Printf("姓名:%s 年龄:%d 成绩:%.1f\n", s.Name, s.Age, s.Score)
}
func main() {
stu := Student{"小明", 18, 92.5}
stu.Show()
}
手机结构体
题目描述
定义
Phone结构体,包含字段:品牌(Brand)、颜色(Color)、价格(Price)实现构造函数
NewPhone初始化对象实现两个方法:
Call()打印:使用【品牌】手机打电话SendMsg()打印:使用【品牌】手机发短信创建对象并调用方法。
输出示例
使用小米手机打电话
使用小米手机发短信
实现代码
Go
package main
import "fmt"
// 手机结构体
type Phone struct {
brand string // 品牌
color string // 颜色
price float64 // 价格
}
// 构造函数
func NewPhone(brand string, color string, price float64) *Phone {
return &Phone{
brand: brand,
color: color,
price: price,
}
}
// 打电话方法
func (p Phone) Call() {
fmt.Printf("使用%s手机打电话\n", p.brand)
}
// 发短信方法
func (p Phone) SendMsg() {
fmt.Printf("使用%s手机发短信\n", p.brand)
}
func main() {
// 创建手机
phone := NewPhone("小米", "白色", 2999)
// 调用方法
phone.Call()
phone.SendMsg()
}
矩形结构体
题目描述
定义 Rect 结构体:长、宽。实现:
- 构造函数
NewRect(width, height float64) *Rect- 方法
Area() float64,返回面积- 方法
Perimeter() float64,返回周长输出长、宽、面积、周长。
输出示例
长:5.0
宽:3.0
面积:15.0
周长:16.0
实现代码
Go
package main
import "fmt"
type Rect struct {
width float64
height float64
}
// 构造函数
func NewRect(width, height float64) *Rect {
return &Rect{
width: width,
height: height,
}
}
// 面积
func (r Rect) Area() float64 {
return r.width * r.height
}
// 周长
func (r Rect) Perimeter() float64 {
return 2 * (r.width + r.height)
}
func main() {
rect := NewRect(5, 3)
fmt.Printf("长:%.1f\n", rect.width)
fmt.Printf("宽:%.1f\n", rect.height)
fmt.Printf("面积:%.1f\n", rect.Area())
fmt.Printf("周长:%.1f\n", rect.Perimeter())
}
矩形计算器
题目描述
定义一个
Rectangle结构体,包含长(length)和宽(width)两个属性。实现以下方法:
area():计算矩形面积
perimeter():计算矩形周长
isSquare():判断是否为正方形
输出示例
矩形1:长=5.0,宽=3.0
面积:15.00
周长:16.00
是否为正方形:false
矩形2:长=4.0,宽=4.0
面积:16.00
周长:16.00
是否为正方形:true
实现代码
Go
package main
import "fmt"
type Rectangle struct {
Length float64
Width float64
}
func (r Rectangle) area() float64 {
return r.Length * r.Width
}
func (r Rectangle) perimeter() float64 {
return 2 * (r.Length + r.Width)
}
func (r Rectangle) isSquare() bool {
return r.Length == r.Width
}
func main() {
rect1 := Rectangle{Length: 5.0, Width: 3.0}
rect2 := Rectangle{Length: 4.0, Width: 4.0}
fmt.Printf("矩形1:长=%.1f,宽=%.1f\n", rect1.Length, rect1.Width)
fmt.Printf("面积:%.2f\n", rect1.area())
fmt.Printf("周长:%.2f\n", rect1.perimeter())
fmt.Printf("是否为正方形:%t\n\n", rect1.isSquare())
fmt.Printf("矩形2:长=%.1f,宽=%.1f\n", rect2.Length, rect2.Width)
fmt.Printf("面积:%.2f\n", rect2.area())
fmt.Printf("周长:%.2f\n", rect2.perimeter())
fmt.Printf("是否为正方形:%t\n", rect2.isSquare())
}
银行账户
题目描述
定义一个
BankAccount结构体,包含账户名(accountName)、账号(accountNumber)、余额(balance)三个属性。实现以下方法:
deposit(amount float64):存款
withdraw(amount float64):取款(不能透支)
getBalance():查询余额
displayInfo():显示账户信息
输出示例
=== 账户信息 ===
账户名:张三
账号:6222020012345678
余额:1000.00元
存款500元后,余额:1500.00元
取款200元后,余额:1300.00元
尝试取款2000元:余额不足!
当前余额:1300.00元
实现代码
Go
package main
import "fmt"
type BankAccount struct {
AccountName string
AccountNumber string
Balance float64
}
func (b *BankAccount) deposit(amount float64) {
if amount > 0 {
b.Balance += amount
fmt.Printf("存款%.2f元后,余额:%.2f元\n", amount, b.Balance)
} else {
fmt.Println("存款金额必须大于0")
}
}
func (b *BankAccount) withdraw(amount float64) {
if amount > 0 && amount <= b.Balance {
b.Balance -= amount
fmt.Printf("取款%.2f元后,余额:%.2f元\n", amount, b.Balance)
} else if amount > b.Balance {
fmt.Printf("尝试取款%.2f元:余额不足!\n", amount)
} else {
fmt.Println("取款金额必须大于0")
}
}
func (b BankAccount) getBalance() float64 {
return b.Balance
}
func (b BankAccount) displayInfo() {
fmt.Println("=== 账户信息 ===")
fmt.Printf("账户名:%s\n", b.AccountName)
fmt.Printf("账号:%s\n", b.AccountNumber)
fmt.Printf("余额:%.2f元\n", b.Balance)
}
func main() {
account := &BankAccount{
AccountName: "张三",
AccountNumber: "6222020012345678",
Balance: 1000.00,
}
account.displayInfo()
fmt.Println()
account.deposit(500)
account.withdraw(200)
account.withdraw(2000)
fmt.Printf("当前余额:%.2f元\n", account.getBalance())
}
学生管理系统
题目描述
定义一个
Student结构体,包含姓名(name)、学号(id)、成绩(scores)三个属性(成绩用切片存储)。实现以下方法:
addScore(score float64):添加成绩
getAverage():计算平均分
getMaxScore():获取最高分
getMinScore():获取最低分
displayInfo():显示学生信息和所有成绩
输出示例
=== 学生信息 ===
姓名:李华
学号:2021001
所有成绩:[85.5 92.0 78.5 88.0]
平均分:86.00
最高分:92.00
最低分:78.50
实现代码
Go
package main
import "fmt"
type Student struct {
Name string
ID string
Scores []float64
}
func (s *Student) addScore(score float64) {
s.Scores = append(s.Scores, score)
fmt.Printf("添加成绩%.1f成功\n", score)
}
func (s Student) getAverage() float64 {
if len(s.Scores) == 0 {
return 0
}
total := 0.0
for _, score := range s.Scores {
total += score
}
return total / float64(len(s.Scores))
}
func (s Student) getMaxScore() float64 {
if len(s.Scores) == 0 {
return 0
}
max := s.Scores[0]
for _, score := range s.Scores {
if score > max {
max = score
}
}
return max
}
func (s Student) getMinScore() float64 {
if len(s.Scores) == 0 {
return 0
}
min := s.Scores[0]
for _, score := range s.Scores {
if score < min {
min = score
}
}
return min
}
func (s Student) displayInfo() {
fmt.Println("=== 学生信息 ===")
fmt.Printf("姓名:%s\n", s.Name)
fmt.Printf("学号:%s\n", s.ID)
fmt.Printf("所有成绩:%v\n", s.Scores)
fmt.Printf("平均分:%.2f\n", s.getAverage())
fmt.Printf("最高分:%.2f\n", s.getMaxScore())
fmt.Printf("最低分:%.2f\n", s.getMinScore())
}
func main() {
student := &Student{
Name: "李华",
ID: "2021001",
Scores: []float64{85.5, 92.0, 78.5},
}
student.addScore(88.0)
fmt.Println()
student.displayInfo()
}
图书管理系统
题目描述
定义一个
Book结构体,包含书名(title)、作者(author)、价格(price)、库存(stock)四个属性。实现以下方法:
sell(quantity int):售出图书(减少库存)
restock(quantity int):补货(增加库存)
setPrice(newPrice float64):修改价格
getValue():计算库存总价值
displayBook():显示图书信息
输出示例
=== 图书信息 ===
书名:《Go语言编程》
作者:张三
价格:59.00元
库存:100本
库存总价值:5900.00元
售出10本后,库存:90本
补货20本后,库存:110本
修改价格为69.00元后,库存总价值:7590.00元
实现代码
Go
package main
import "fmt"
type Book struct {
Title string
Author string
Price float64
Stock int
}
func (b *Book) sell(quantity int) {
if quantity > 0 && quantity <= b.Stock {
b.Stock -= quantity
fmt.Printf("售出%d本后,库存:%d本\n", quantity, b.Stock)
} else if quantity > b.Stock {
fmt.Printf("库存不足!当前库存只有%d本\n", b.Stock)
} else {
fmt.Println("售出数量必须大于0")
}
}
func (b *Book) restock(quantity int) {
if quantity > 0 {
b.Stock += quantity
fmt.Printf("补货%d本后,库存:%d本\n", quantity, b.Stock)
} else {
fmt.Println("补货数量必须大于0")
}
}
func (b *Book) setPrice(newPrice float64) {
if newPrice > 0 {
b.Price = newPrice
fmt.Printf("修改价格为%.2f元后,", b.Price)
}
}
func (b Book) getValue() float64 {
return b.Price * float64(b.Stock)
}
func (b Book) displayBook() {
fmt.Println("=== 图书信息 ===")
fmt.Printf("书名:《%s》\n", b.Title)
fmt.Printf("作者:%s\n", b.Author)
fmt.Printf("价格:%.2f元\n", b.Price)
fmt.Printf("库存:%d本\n", b.Stock)
fmt.Printf("库存总价值:%.2f元\n", b.getValue())
}
func main() {
book := &Book{
Title: "Go语言编程",
Author: "张三",
Price: 59.00,
Stock: 100,
}
book.displayBook()
fmt.Println()
book.sell(10)
book.restock(20)
book.setPrice(69.00)
fmt.Printf("库存总价值:%.2f元\n", book.getValue())
}
计数器
题目描述
定义一个
Counter结构体,包含计数(count)属性。实现以下方法:
increment():计数加1
decrement():计数减1(不能小于0)
reset():重置计数为0
getCount():获取当前计数
display():显示计数
输出示例
初始计数:0
增加3次后:3
减少1次后:2
重置后:0
实现代码
Go
package main
import "fmt"
type Counter struct {
count int
}
func (c *Counter) increment() {
c.count++
}
func (c *Counter) decrement() {
if c.count > 0 {
c.count--
}
}
func (c *Counter) reset() {
c.count = 0
}
func (c Counter) getCount() int {
return c.count
}
func (c Counter) display() {
fmt.Printf("当前计数:%d\n", c.count)
}
func main() {
counter := &Counter{count: 0}
fmt.Printf("初始计数:%d\n", counter.getCount())
counter.increment()
counter.increment()
counter.increment()
fmt.Printf("增加3次后:%d\n", counter.getCount())
counter.decrement()
fmt.Printf("减少1次后:%d\n", counter.getCount())
counter.reset()
fmt.Printf("重置后:%d\n", counter.getCount())
}