在此 Go 语言计算电费程序中,我们假设您的电力公司根据用电量收取不同的费率。在此 Go 示例中,我们使用 Else If 语句,
- else if units <= 100 – 前五十个单位的费用为 130(2.60 * 50)。因此,我们添加了该金额,并从总数中消除了这五十个单位。
- else if units <= 200 – 前五十个单位的费用为 130,50 到 100 个单位的费用为 162.50(3.25 * 50)。因此,我们添加了该金额,并消除了总数中的这 100 个单位。
- else – 前五十个单位的费用为 130,50 到 100 个单位的费用为 162.50,100 到 200 个单位的费用为 526(5.65 * 100)。因此,我们附加了该金额,并消除了总数中的这两百个单位。
package main
import "fmt"
func main() {
var units, surCharge, amount, totAmount float64
fmt.Print("Enter the Consumed Units = ")
fmt.Scanln(&units)
if units < 50 {
amount = units * 2.60
surCharge = 25
} else if units <= 100 {
amount = 130 + ((units - 50) * 3.25)
surCharge = 35
} else if units <= 100 {
amount = 130 + 162.50 + ((units - 100) * 5.26)
surCharge = 45
} else {
amount = 130 + +162.50 + 526 + ((units - 200) * 7.75)
surCharge = 55
}
totAmount = amount + surCharge
fmt.Println("Electricity Bill = ", totAmount)
}

Golang 语言计算电费程序
在此 Go 示例中,我们假设您对单位有统一费率或固定费率,并计算电费。
package main
import "fmt"
func main() {
var units, surCharge, amount, totAmount float64
fmt.Print("Enter the Consumed Units = ")
fmt.Scanln(&units)
if units > 500 {
amount = units * 12.65
surCharge = 125
} else if units >= 300 {
amount = units * 10.75
surCharge = 100
} else if units >= 200 {
amount = units * 8.26
surCharge = 85
} else if units >= 100 {
amount = units * 5.98
surCharge = 65
} else {
amount = units * 3.85
surCharge = 45
}
totAmount = amount + surCharge
fmt.Println("Electricity Bill = ", totAmount)
}
SureshMac:Goexamples suresh$ go run electricity2.go
Enter the Consumed Units = 720
Electricity Bill = 9233
SureshMac:Goexamples suresh$ go run electricity2.go
Enter the Consumed Units = 400
Electricity Bill = 4400
SureshMac:Goexamples suresh$ go electricity2.go
Enter the Consumed Units = 65
Electricity Bill = 295.25