英文:
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 "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)
}
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
及其方法的文档,并查看示例。
例如,“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.
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 (
"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!")
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论