我在我的Go代码中遇到了输出问题。

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

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

几件事情:

  1. fmt.Print 函数不会在你打印的字符串后添加换行符。所以你可能想把像 fmt.Print("Name: ") 这样的行改成 fmt.Println("Name:") 或者 fmt.Print("Name:\n")
  2. 第一点会有所帮助,但你可能还想看看 fmt.Scanf 函数。它允许你提供一个格式字符串,比如 fmt.Scanf("%d", &age1),来明确地从输入中查找整数或整数部分。
  3. 你把 name1name2 变量声明为 int 类型。我认为它们应该是 string 类型。
  4. 在打印平均值的那一行中,删除变量前面的 &。在那里不需要它。

希望这能让你更接近你想要的结果。

英文:

A couple of things:

  1. The fmt.Print function does not add a newline character to the string you're printing. So you probably want to change lines like fmt.Print("Name: ") to fmt.Println("Name:") or fmt.Print("Name:\n").
  2. #1 will help here, but you may want to look at the fmt.Scanf function. It lets you provide a format string like this fmt.Scanf("%d", &age1) to explicitly look for an integer or whole number in the input.
  3. You've declared the name1 and name2 variables as int. I think they should be string.
  4. 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% 

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

发表评论

匿名网友

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

确定