英文:
I'm having dificulties with output problems in my Go code
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是一个新手,也是一个编程新手。我正在尝试通过做一些练习来提高我的编程逻辑,我喜欢使用Python和Go这些我喜欢的编程语言。
在这个特定的例子中,我的问题出在Go语言上。
代码的目标是打印以下内容:
Example
First person info:
Name: Mary
Age: 19
Second person info:
Name: John
Age: 20
The average age of Mary and John is 19.5 years
为了解决这个练习,我尝试了以下代码:
package main
import "fmt"
func main() {
var name1 string
var name2 string
var age1, age2 int
var average float32
fmt.Println("First person info:")
fmt.Print("Name: ")
fmt.Scan(&name1)
fmt.Print("Age: ")
fmt.Scan(&age1)
fmt.Println("Second person info:")
fmt.Print("Name: ")
fmt.Scan(&name2)
fmt.Print("Age: ")
fmt.Scan(&age2)
average = float32(age1+age2) / 2
fmt.Printf("\nThe average age of %s and %s is %.1f years", name1, name2, average)
}
然而,输出结果却是这样的:
First person info:
Name: Mary
Age: Second person info:
Name: Age:
The average age of %s and %s is %.2f years 0xc000018088 0xc0000180a0 0xc0000180b8
PS C:\Users\alexi\Meu Drive\Programação\golang>
所以,代码没有逐个显示print
函数。我先回答了第一个名字(name1
),然后输出结果就把其他所有的内容都一起输出了(年龄、第二个人的信息等等)。我尝试了各种方法,但是无法理解可能出了什么问题...
英文:
I'm new here and to programming. I am trying to improve my programming logic by doing some exercises in my preferred languages such as Python and Go.
In this particular case, my issue is with Go.
The code basically should print the following:
Example
First person info:
Name: Mary
Age: 19
Second person info:
Name: John
Age: 20
The average age of Mary and John is 19.5 years
To solve this exercise, I tried the following code:
package main
import "fmt"
func main() {
var name1 int
var name2 int
var age1, age2 int
var average float32
fmt.Println("First person info:")
fmt.Print("Name: ")
fmt.Scan(&name1)
fmt.Print("Age: ")
fmt.Scan(&age1)
fmt.Println("Second person info:")
fmt.Print("Name: ")
fmt.Scan(&name2)
fmt.Print("Age: ")
fmt.Scan(&age2)
average = float32((age1 + age2) / 2)
fmt.Printf("\nThe average age of %d and %d is %f years", &name1, &name2, &average)
}
I've tried some variations, trying to convert some variables, but the thing is that the output is like below:
First person info:
Name: Mary
Age: Second person info:
Name: Age:
The average age of %s and %s is %.2f years 0xc000018088 0xc0000180a0 0xc0000180b8
PS C:\Users\alexi\Meu Drive\Programação\golang>
So, the code don't show the print
function one by one. I answer the first name (name1
) and soon I answer it, the output brings all others, all together (age, second person info etc.) I tried everything and can't understand what possibly went wrong...
答案1
得分: 1
几件事情:
fmt.Print
函数不会在你打印的字符串后添加换行符。所以你可能想把像fmt.Print("Name: ")
这样的行改成fmt.Println("Name:")
或者fmt.Print("Name:\n")
。- 第一点会有所帮助,但你可能还想看看
fmt.Scanf
函数。它允许你提供一个格式字符串,比如fmt.Scanf("%d", &age1)
,来明确地从输入中查找整数或整数部分。 - 你把
name1
和name2
变量声明为int
类型。我认为它们应该是string
类型。 - 在打印平均值的那一行中,删除变量前面的
&
。在那里不需要它。
希望这能让你更接近你想要的结果。
英文:
A couple of things:
- The
fmt.Print
function does not add a newline character to the string you're printing. So you probably want to change lines likefmt.Print("Name: ")
tofmt.Println("Name:")
orfmt.Print("Name:\n")
. - #1 will help here, but you may want to look at the
fmt.Scanf
function. It lets you provide a format string like thisfmt.Scanf("%d", &age1)
to explicitly look for an integer or whole number in the input. - You've declared the
name1
andname2
variables asint
. I think they should bestring
. - Remove the
&
before the variables in the line where you print out the average. It's not needed there.
Hopefully, that gets you closer to where you want to be.
答案2
得分: -1
package main
import (
"fmt"
"strconv"
)
func main() {
var name1, age1, name2, age2 string
var average float32
fmt.Println("第一个人的信息:")
fmt.Print("姓名:")
fmt.Scan(&name1)
fmt.Print("年龄:")
fmt.Scan(&age1)
fmt.Println("第二个人的信息:")
fmt.Print("姓名:")
fmt.Scan(&name2)
fmt.Print("年龄:")
fmt.Scan(&age2)
ageInt1, _ := strconv.Atoi(age1)
ageInt2, _ := strconv.Atoi(age2)
average = float32((ageInt1 + ageInt2) / 2)
fmt.Printf("\n%s和%s的平均年龄是%f岁", name1, name2, average)
}
//输出结果将会是:
//第一个人的信息:
//姓名:Joh
//年龄:23
//第二个人的信息:
//姓名:Doe
//年龄:34
//
//Joh和Doe的平均年龄是28.000000岁%
英文:
package main
import (
"fmt"
"strconv"
)
func main() {
var name1, age1 , name2, age2 string
var average float32
fmt.Println("First person info:")
fmt.Print("Name: ")
fmt.Scan(&name1)
fmt.Print("Age: ")
fmt.Scan(&age1)
fmt.Println("Second person info:")
fmt.Print("Name: ")
fmt.Scan(&name2)
fmt.Print("Age: ")
fmt.Scan(&age2)
ageInt1, _ := strconv.Atoi(age1)
ageInt2, _ := strconv.Atoi(age2)
average = float32(( ageInt1+ ageInt2) / 2)
fmt.Printf("\nThe average age of %v and %v is %f years", name1, name2, average)
}
//output will be like
//enter code here
//First person info:
//Name: Joh
//Age: 23
//Second person info:
//Name: Doe
//Age: 34
//The average age of Joh and Doe is 28.000000 years%
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论