我正在尝试猜测一个数字,但是我的程序无法正确解释数字。

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

GO:I am trying to make a guess My number, but my program will not interpret numbers correctly

问题

package main

import "fmt"
import bf "bufio"
import "os"
import "strconv"

type SVC int

func main() {
    fmt.Println("Loaded")
    var gmber = bf.NewScanner(os.Stdin)
    gmber.Scan()
    i := 1
    for i < 40 {
        fmt.Println("\n")
        i++
    }
    var input2 = bf.NewScanner(os.Stdin)
    fmt.Println("Make a guess:")
    var input21, err = strconv.Atoi(input2.Text())
    var gmber1, err0 = strconv.Atoi(gmber.Text())
    input2.Scan()
    for {
        if input21 == gmber1 {
            break
        }
        if input21 > gmber1 {
            fmt.Println("Too high, Guess again")
            input2.Scan()
        }
        if input21 < gmber1 {
            fmt.Println("Too low, Guess again")
            input2.Scan()
        }
    }
    fmt.Println("You win!")
    fmt.Println(err)
    fmt.Println(err0)
}

当我运行这个程序时,它总是输出"Too low, Guess again"。我输入了100作为要猜测的数字,然后猜测了101,它说太低了。我真的不知道该怎么办,有人知道为什么这个程序会这样吗?

注意:
input21 总是等于 0

英文:
package main

import &quot;fmt&quot;
import bf &quot;bufio&quot;
import &quot;os&quot;
import &quot;strconv&quot;

type SVC int

func main() {
        fmt.Println(&quot;Loaded&quot;)
        var gmber = bf.NewScanner(os.Stdin)
        gmber.Scan()
        i := 1
        for i &lt; 40 {
                fmt.Println(&quot;\n&quot;)
                i++
        }
        var input2 = bf.NewScanner(os.Stdin)
        fmt.Println(&quot;Make a guess:&quot;)
        var input21, err = strconv.Atoi(input2.Text())
        var gmber1, err0 = strconv.Atoi(gmber.Text())
        input2.Scan()
        for {
                if input21 == gmber1{
                        break
                }
                if input21 &gt; gmber1 {
                        fmt.Println(&quot;Too high, Guess again&quot;)
                        input2.Scan()
                }
                if input21 &lt; gmber1 {
                        fmt.Println(&quot;Too low, Guess again&quot;)
                        input2.Scan()
                }
        }
        fmt.Println(&quot;You win!&quot;)
        fmt.Println(err)
        fmt.Println(err0)
}

I when I run this program, my program will always spit out "Too low, Guess again". I input: 100 as the number to be guessed, then guessed the number 101, and it said too low. I just do not really know what to do now, does anybody understand why this program does this?
NOTES:
input21 is ALWAYS=to 0

答案1

得分: 2

你需要了解如何使用类型Scanner。仔细阅读type Scanner及其方法的文档,并查看示例。

package bufio: type Scanner

例如,“Scan方法将Scanner推进到下一个标记,然后可以通过Bytes或Text方法访问该标记。”

对于缓冲流输入,每个文件最好只有一个扫描器。

猜自己输入的数字并不有趣。可以使用随机数生成器,例如package math/rand

解释你的代码中所有问题需要太长时间,所以这里有一个简单的示例:

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	var number = rand.Intn(100)
	fmt.Println("Make a guess:")
	var scanner = bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		var guess, err = strconv.Atoi(scanner.Text())
		if err != nil {
			fmt.Print("Input error")
		} else if guess == number {
			break
		} else if guess > number {
			fmt.Print("Too high")
		} else if guess < number {
			fmt.Print("Too low")
		}
		fmt.Println(", Guess again:")
	}
	if scanner.Err() != nil {
		fmt.Println("I give up!")
	}
	fmt.Println("You win!")
}
英文:

You need to understand how type Scanner works. Read the documentation for type Scanner and its methods carefully and take a look at the examples.

package bufio: type Scanner

For example, "Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method."

For buffered stream input, it's dangerous to have more than one scanner per file.

It's no fun to guess a number that you input yourself. Use a random number generator, for example, 'package math/rand`.

It would take too long to explain all the problems with your code, so here's a simple example,

package main

import (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;os&quot;
	&quot;strconv&quot;
	&quot;time&quot;
)

func main() {
	rand.Seed(time.Now().UnixNano())
	var number = rand.Intn(100)
	fmt.Println(&quot;Make a guess:&quot;)
	var scanner = bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		var guess, err = strconv.Atoi(scanner.Text())
		if err != nil {
			fmt.Print(&quot;Input error&quot;)
		} else if guess == number {
			break
		} else if guess &gt; number {
			fmt.Print(&quot;Too high&quot;)
		} else if guess &lt; number {
			fmt.Print(&quot;Too low&quot;)
		}
		fmt.Println(&quot;, Guess again:&quot;)
	}
	if scanner.Err() != nil {
		fmt.Println(&quot;I give up!&quot;)
	}
	fmt.Println(&quot;You win!&quot;)
}

答案2

得分: 2

每当你有一个返回error值的函数时,你必须检查它是否为nil。只有在你完成这个检查之后,才能访问函数返回的其他任何值。

当你添加了正确的错误处理后,你将会看到程序失败的地方。

英文:

Whenever you have a function that returns an error value, you must check whether that is nil. Only after you have done that may you access any other value that the function returned.

When you add this proper error handling, you will see where your program fails.

答案3

得分: 0

在if语句的末尾,重新评估扫描仪时,我没有重置扫描仪,你需要重新声明strconv.Atoi。

英文:

I was not resetting the scanners, at the end of the if's where the scanner is re-evaluated, you have to re-declare the strconv.Atoi

huangapple
  • 本文由 发表于 2016年3月21日 06:51:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/36120621.html
匿名

发表评论

匿名网友

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

确定