Go 戈朗浮回零

Go 戈朗浮回零,go,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 :=

为什么此代码总是返回零:

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

}
主程序包
输入“fmt”
func main(){
数组int的变量索引
fmt.Scan(&索引数组)
var arr=make([]int,数组的索引)
对于i:=0;i0{
肯定的++
}否则,如果arr_v<0{
否定的++
}否则,如果arr_v==0{
零_++
}
}
fmt.Printf(“%f”,浮点32(正/长(arr)))
fmt.Printf(“%f”,浮点32(负片/透镜(arr)))
fmt.Printf(“%f”,float32(零/长(arr)))
}
我的意思是输出

表示数组中正数分数的十进制数。 表示数组中负数分数的十进制数。 表示数组中零的分数的十进制数

样本输入

六,

-43-9041

样本输出

50万

0.333

0.166667

但在我的代码中,类似这样的输出使用相同的输入

百万

百万


0.000000

首先,从用户处获得的第一个变量是数组的长度,而不是索引。。。第二,当要求用户输入时,应提示用户,如下所示:

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

试试看

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

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

}

如果您不提供您正在键入的输入和应用程序的输出,则很难回答这个问题,因为
float32(zero\len(arr))
首先将一个int除以一个int,得到0,然后将该0转换为float32 0。它应该是
float32(零)/float32(len(arr))
我的意思是输出一个表示数组中正数分数的十进制数。表示数组中负数分数的十进制数。表示数组中零的分数的十进制数。示例输入6-4 3-9 0 4 1示例输出0.500000 0.333333 0.166667,但在我的代码中,类似这样的输出与相同的输入1.0000000.0000000.000000Welcome。很高兴我的回答能帮助你。
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)))

}
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