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

浮点数在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条)

相关推荐 更多相似问题