Float return zero in golang

huangapple go评论95阅读模式
英文:

Float return zero in golang

问题

为什么这段代码总是返回零:

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

英文:

Why does this code always return zero:

package main

import &quot;fmt&quot;

func main() {
	var index_of_array int
	fmt.Scan(&amp;index_of_array)
	var arr = make([]int, index_of_array)
	for i := 0; i &lt; len(arr); i++ {
		fmt.Scan(&amp;arr[i])
	}

	positive := 0
	negative := 0
	zero_ := 0
	for _, arr_v := range arr {
		if arr_v &gt; 0 {
			positive++
		} else if arr_v &lt; 0 {
			negative++
		} else if arr_v == 0 {
			zero_++
		}
	}

	fmt.Printf(&quot;%f &quot;, float32(positive/len(arr)))
	fmt.Printf(&quot;%f &quot;, float32(negative/len(arr)))
	fmt.Printf(&quot;%f &quot;, 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

答案1

得分: 0

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

package main

import "fmt"

func main() {
  var length int
  fmt.Print("请输入数组的长度:")
  fmt.Scan(&length)
  
  var arr = make([]int, length)
  for i := 0; i < len(arr); i++ {
    fmt.Printf("请输入数组索引 %d 处的值:", i)
    fmt.Scan(&arr[i])
  }
  
  fmt.Println("您输入的数组为:", 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("数组中有 %d 个正数\n", int32(positive))
  fmt.Printf("数组中有 %d 个负数\n", int32(negative))
  fmt.Printf("数组中有 %d 个零", int32(zero))
}

示例用法:

请输入数组的长度:4
请输入数组索引 0 处的值:7
请输入数组索引 1 处的值:0
请输入数组索引 2 处的值:-9
请输入数组索引 3 处的值:8
您输入的数组为:[7 0 -9 8]
数组中有 2 个正数
数组中有 1 个负数
数组中有 1 个零

在此处尝试:点击这里!

英文:

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 &quot;fmt&quot;

func main() {
  var length int
  fmt.Print(&quot;Please enter the length of the array: &quot;)
  fmt.Scan(&amp;length)
  
  var arr = make([]int, length)
  for i := 0; i &lt; len(arr); i++ {
    fmt.Printf(&quot;Please enter the value at index %d of the array: &quot;, i)
    fmt.Scan(&amp;arr[i])
  }
  
  fmt.Println(&quot;You entered the array: &quot;, arr)
  
  positive := 0
  negative := 0
  zero := 0
  for _, arr_v := range arr {
    if arr_v &gt; 0 {
      positive++
    } else if arr_v &lt; 0 {
      negative++
    } else if arr_v == 0 {
      zero++
    }
  }
  
  fmt.Printf(&quot;There are %d postive numbers in the array\n&quot;, int32(positive))
  fmt.Printf(&quot;There are %d negative numbers in the array\n&quot;, int32(negative))
  fmt.Printf(&quot;There are %d zeroes in the array&quot;, 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!

答案2

得分: 0

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

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	fmt.Print("输入数组的长度:")
	scanner.Scan()
	index_of_array, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic("错误!")
	}

	arr := make([]int, index_of_array)

	for i := 0; i < len(arr); i++ {
		fmt.Printf("输入第 %d 个值:", i+1)
		scanner.Scan()
		arr[i], err = strconv.Atoi(scanner.Text())
		if err != nil {
			panic("错误 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("输入的数组:", arr)
	fmt.Printf("数组中有 %d 个正数,占比 %.6f\n", positive, float64(positive)/float64(len(arr)))
	fmt.Printf("数组中有 %d 个负数,占比 %.6f\n", negative, float64(negative)/float64(len(arr)))
	fmt.Printf("数组中有 %d 个零,占比 %.6f\n", zero_, float64(zero_)/float64(len(arr)))

}

你可以在 repl.it 上运行它。

你可以像下面的例子一样使用它:

输入数组的长度:6
输入第 1 个值:-4
输入第 2 个值:3
输入第 3 个值:-9
输入第 4 个值:0
输入第 5 个值:4
输入第 6 个值:1
输入的数组: [-4 3 -9 0 4 1]
数组中有 3 个正数,占比 0.500000
数组中有 2 个负数,占比 0.333333
数组中有 1 个零,占比 0.166667
英文:

I've done some modifications to your code and it should run as you expected:

package main

import (
	&quot;fmt&quot;
	&quot;bufio&quot;
	&quot;os&quot;
	&quot;strconv&quot;
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	fmt.Print(&quot;Enter the lenght of the array: &quot;)
	scanner.Scan()
	index_of_array, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic(&quot;Errroor!&quot;)
	}

    arr := make([]int, index_of_array)

    for i := 0; i &lt; len(arr); i++ {
    	fmt.Printf(&quot;Enter value number %d: &quot;, i+1)
        scanner.Scan()
        arr[i], err = strconv.Atoi(scanner.Text())
        if err != nil {
        	panic(&quot;Error 2!&quot;)
        }
    }

    positive := 0
    negative := 0
    zero_ := 0
    for _, arr_v := range arr {
        if arr_v &gt; 0 {
            positive++
        } else if arr_v &lt; 0 {
            negative++
        } else if arr_v == 0 {
            zero_++
        }
    }

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

huangapple
  • 本文由 发表于 2017年2月12日 10:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/42183615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定