0
我正在努力学习Go并创建了一个函数,我声明了一个变量game_ratio并将其设置为0.0。然后我有一个if语句,我尝试更新game_ratio的值。当我尝试编译,我得到以下错误信息: “game_ratio声明,不使用”如何在Go中的if语句中更新变量的值?
这里是我的功能:
func gameRatio(score1 int, score2 int, max_score float64) float64 {
var game_ratio float64 = 0.0
var scaled_score_1 = scaleScore(score1, max_score)
var scaled_score_2 = scaleScore(score2, max_score)
fmt.Printf("Scaled score for %v is %v\n", score1, scaled_score_1)
fmt.Printf("Scaled score for %v is %v\n", score2, scaled_score_2)
if score1 > score2 {
game_ratio := (scaled_score_1+1.0)/(scaled_score_1+scaled_score_2+2.0) + 1.0*0.5
}
return game_ratio
}
下面的代码来调用它:
func main() {
flag.Parse()
s1 := flag.Arg(0)
s2 := flag.Arg(1)
i1, err := strconv.Atoi(s1)
i2, err := strconv.Atoi(s2)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
fmt.Println("Game ratio is", gameRatio(i1, i2, 6))
}
ScaleScore是我写的另一个函数。如果我删除了if语句,代码就可以工作。
运行我的应用程序,我键入“排名28 24”
Jeez。完全没有注意到。谢谢! – user338413