【问题标题】:How to fix %!(EXTRA int=3) error in Golang? [closed]如何修复 Golang 中的 %!(EXTRA int=3) 错误? [关闭]
【发布时间】:2020-04-25 11:44:03
【问题描述】:

我对 Golang 很陌生,我想知道为什么每次运行代码时都会出现这个错误
%!(EXTRA int=3)

...
var money int
var buyItem string
fmt.Print("Inserisci il nome dell'arma che vuoi comprare: ")
fmt.Scan(&buyItem)
switch buyItem {
        case "Racchetta":
            fmt.Println("Hai comprato con successo la racchetta")
            money = money - 1
            fmt.Printf("Soldi rimanenti:", money)
        case "Granata":
            fmt.Println("Hai comprato con successo la granata")
            money = money - 7
            fmt.Printf("Soldi rimanenti:", money)
        case "AK":
            fmt.Println("Hai comprato con successo l'AK")
            money = money - 12
            fmt.Printf("Soldi rimanenti:", money)
        default:
            fmt.Println("Non hai inserito il nome dell'arma corretto")
}

当我运行它时,我得到这个错误:Soldi rimanenti:%!(EXTRA int=3) 谁能帮帮我?

【问题讨论】:

  • Printf 和 Println 是有区别的。查阅文档。
  • 是的,我知道区别,我分心了,我忘记了 Printf 需要 %v、%d 或 %f 等。

标签: go int extra


【解决方案1】:

您需要将格式化程序添加到您的字符串中,例如

fmt.Printf("Soldi rimanenti: %v\n", money)

%v 是通用格式化程序,适用于大多数情况,但还有更多。在这里查看示例https://gobyexample.com/string-formatting

\n 用于在打印后添加换行。

【讨论】:

  • 哦,非常感谢,我忘记了
猜你喜欢
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 2012-03-24
  • 2014-12-11
  • 2019-04-07
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多