golang

时间:2017-02-12 02:32:12

标签: go

为什么此代码始终返回零:

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)))

}

我的意思是输出

小数表示数组中正数的分数。  小数表示数组中负数的分数。  小数表示数组中零的分数。

示例输入

6

-4 3 -9 0 4 1

示例输出

0.500000

0.333333

0.166667

但在我的代码中输出像这样具有相同的输入

0.000000

0.000000

0.000000

2 个答案:

答案 0 :(得分:0)

首先,你从用户那里得到的第一个变量是数组的长度而不是索引......其次你应该在询问用户输入时提示用户:

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\n", int32(positive))
  fmt.Printf("There are %d negative numbers in the array\n", int32(negative))
  fmt.Printf("There are %d zeroes in the array", int32(zero))
}

使用示例:

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

试试here!

答案 1 :(得分:0)

我对您的代码进行了一些修改,它应该按照您的预期运行:

Calendar

您可以在repl.it

上运行它

你可以像这个例子一样使用它:

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\n", positive, len(arr), float64(positive)/float64(len(arr)))
    fmt.Printf("There are %d negative number(s) of %d in the array. Fraction: %.6f\n", negative, len(arr), float64(negative)/float64(len(arr)))
    fmt.Printf("There are %d zero(s) of %d in the array. Fraction: %.6f\n", zero_, len(arr), float64(zero_)/ float64(len(arr)))

}