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

浮点数在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 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题