「已注销」 2017-02-12 02:32
浏览 152
已采纳

浮点数在Golang中返回零

Why does this code always return zero:

package main

import "fmt"

func main() {
    var index_of_array int
    fmt.Scan(&index_of_array)
    var arr = make([]int, index_of_array)
    for i := 0; i < len(arr); i++ {
        fmt.Scan(&arr[i])
    }

    positive := 0
    negative := 0
    zero_ := 0
    for _, arr_v := range arr {
        if arr_v > 0 {
            positive++
        } else if arr_v < 0 {
            negative++
        } else if arr_v == 0 {
            zero_++
        }
    }

    fmt.Printf("%f ", float32(positive/len(arr)))
    fmt.Printf("%f ", float32(negative/len(arr)))
    fmt.Printf("%f ", float32(zero_/len(arr)))

}

i mean to output

A decimal representing of the fraction of positive numbers in the array. A decimal representing of the fraction of negative numbers in the array. A decimal representing of the fraction of zeroes in the array.

Sample Input

6

-4 3 -9 0 4 1

Sample Output

0.500000

0.333333

0.166667

but in my code the output like this with the same input

0.000000

0.000000

0.000000

  • 写回答

2条回答 默认 最新

  • dsy19811981 2017-02-12 03:10
    关注

    I've done some modifications to your code and it should run as you expected:

    package main
    
    import (
        "fmt"
        "bufio"
        "os"
        "strconv"
    )
    
    func main() {
        scanner := bufio.NewScanner(os.Stdin)
        fmt.Print("Enter the lenght of the array: ")
        scanner.Scan()
        index_of_array, err := strconv.Atoi(scanner.Text())
        if err != nil {
            panic("Errroor!")
        }
    
        arr := make([]int, index_of_array)
    
        for i := 0; i < len(arr); i++ {
            fmt.Printf("Enter value number %d: ", i+1)
            scanner.Scan()
            arr[i], err = strconv.Atoi(scanner.Text())
            if err != nil {
                panic("Error 2!")
            }
        }
    
        positive := 0
        negative := 0
        zero_ := 0
        for _, arr_v := range arr {
            if arr_v > 0 {
                positive++
            } else if arr_v < 0 {
                negative++
            } else if arr_v == 0 {
                zero_++
            }
        }
    
        fmt.Println("Array entered: ", arr)
        fmt.Printf("There are %d positive number(s) of %d in the array. Fraction: %.6f
    ", positive, len(arr), float64(positive)/float64(len(arr)))
        fmt.Printf("There are %d negative number(s) of %d in the array. Fraction: %.6f
    ", negative, len(arr), float64(negative)/float64(len(arr)))
        fmt.Printf("There are %d zero(s) of %d in the array. Fraction: %.6f
    ", zero_, len(arr), float64(zero_)/ float64(len(arr)))
    
    }
    

    you can run it on repl.it

    And you can use it like this example:

    Enter the lenght of the array: 6
    Enter value number 1: -4
    Enter value number 2: 3
    Enter value number 3: -9
    Enter value number 4: 0
    Enter value number 5: 4
    Enter value number 6: 1
    Array entered:  [-4 3 -9 0 4 1]
    There are 3 positive number(s) of 6 in the array. Fraction: 0.500000
    There are 2 negative number(s) of 6 in the array. Fraction: 0.333333
    There are 1 zero(s) of 6 in the array. Fraction: 0.166667
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应