为什么Go给我这个结果?

huangapple go评论68阅读模式
英文:

Why does go give me this result?

问题

我正在制作一个计算班级男女比例的程序,但是它给出了错误的结果。

代码如下:

package main
import {
    "fmt"
}

var total, mujeres, hombres float64

func main() {
    fmt.Printf("Número de mujeres:")
    fmt.Scanln(&mujeres)

    fmt.Printf("Número de hombres:")
    fmt.Scanln(&hombres)

    total = mujeres + hombres
    mujeres = (mujeres / total) * 100
    hombres = (hombres / total) * 100

    print("En al salón de clases hay ", mujeres, "% de mujeres y ", 
        hombres, "% de hombres")
}

当我输入50作为两个数量时,输出结果如下:

En al salón de clases hay +5.000000+001% de mujeres y +5.000000+001% de hombres

我想知道是什么原因导致了这个问题,以及如何解决它。

英文:

I am making a program that calculates the percentage of males and females in the class. But it gives me an incorrect result.

The code is:

package main
import {
    "fmt"
}

var total, mujeres, hombres float64

func main() {
    fmt.Printf("Número de mujeres:")
    fmt.Scanln(&mujeres)

    fmt.Printf("Número de hombres:")
    fmt.Scanln(&hombres)

    total = mujeres + hombres
    mujeres = (mujeres / total) * 100
    hombres = (hombres / total) * 100

    print("En al salón de clases hay ", mujeres, "% de mujeres y ", 
        hombres, "% de hombres")
}

And the output I'm getting when entering 50 for both quantities is:

En al salón de clases hay +5.000000+001% de mujeres y +5.000000+001% de hombres

I want to know what causes this problem and how to solve it.

答案1

得分: 0

它并没有给出错误的结果,而是以错误的格式给出了正确的结果。 +5.000000e+0015x10^1,等于 50

如果你想以不同于默认格式的方式进行格式化,你需要指定,例如:

fmt.Printf("En al salón de clases hay %.1f%% du mujeres y %.1f%% du hombres\n",
    mujeres, hombres)
英文:

It's not giving an incorrect result, it's giving the correct result in an incorrect format. The value +5.000000e+001 is <code>5x10<sup>1</sup></code>, which is equal to 50.

If you want them formatted differently to the default, you need to specify that, such as with:

fmt.Printf(&quot;En al sal&#243;n de clases hay %.1f%% du mujeres y %.1f%% du hombres\n&quot;,
    mujeres, hombres)

huangapple
  • 本文由 发表于 2023年2月6日 06:29:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定