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

浮点数在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
    
    打赏 评论
  • douxiong2999 2017-02-12 03:01

    Firstly the first variable you get from the user is the length of the array not the index... Secondly you should prompt the user when asking for user input like so:

    package main
    
    import "fmt"
    
    func main() {
      var length int
      fmt.Print("Please enter the length of the array: ")
      fmt.Scan(&length)
    
      var arr = make([]int, length)
      for i := 0; i < len(arr); i++ {
        fmt.Printf("Please enter the value at index %d of the array: ", i)
        fmt.Scan(&arr[i])
      }
    
      fmt.Println("You entered the array: ", arr)
    
      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("There are %d postive numbers in the array
    ", int32(positive))
      fmt.Printf("There are %d negative numbers in the array
    ", int32(negative))
      fmt.Printf("There are %d zeroes in the array", int32(zero))
    }
    

    Example Usage:

    Please enter the length of the array:  4
    Please enter the value at index 0 of the array:  7
    Please enter the value at index 1 of the array:  0
    Please enter the value at index 2 of the array:  -9
    Please enter the value at index 3 of the array:  8
    You entered the array:  [7 0 -9 8]
    There are 2 postive numbers in the array
    There are 1 negative numbers in the array
    There are 1 zeroes in the array
    

    Try it here!

    打赏 评论

相关推荐 更多相似问题