Syntax for me to input values in Go Language

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

Syntax for me to input values in Go Language

问题

我一直在寻找一种在C语言中实现以下语法的方法:

scanf("%d",&num);

我发现在Go语言中可以使用以下语法:

fmt.Scanf("%d",&num)

问题是,当我使用它两次时,它不允许我在第二部分输入值。

示例:

package main

import "fmt"

func main() {
    var name string
    var age int
    fmt.Print("Enter your name: ")
    fmt.Scanf("%s", &name)
    fmt.Print("\nEnter your age: ")
    fmt.Scanf("%d", &age)
    fmt.Println(name,"is",age,"years of Age")
}

输入示例:name=Jack,age=18(这是问题的一部分,它不允许我为年龄输入值)
它将显示:

Jack is 0 years of age
英文:

I've been looking for a way how to have a syntax like this in C

scanf("%d",&num);

and I've found out in Go Lanuguage you can use this

fmt.scanf("%d",&num)

The problem is when I'll use it twice, it doesn't allow me to enter a value on the second part.

Example:

package main

import "fmt"

func main() {
    var name string
    var age int
    fmt.Print("Enter your name: ")
    fmt.Scanf("%s", &name)
    fmt.Print("\nEnter your age: ")
    fmt.Scanf("%d", &age)
    fmt.Println(name,"is",age,"years of Age")
}

Sample input name=Jack and age=18(this is the part of the problem wherein it doesn't allow me to input for the age)
it will display

Jack is 0 years of age

答案1

得分: 1

你可以将最后一行替换为以下内容:

fmt.Printf("%s的年龄是%d岁", name, age)

以下是完整的代码:

package main                                                                                                                            

import "fmt"

func main() {
    var name string
    var age int 
    fmt.Print("请输入你的名字:")
    fmt.Scanf("%s", &name)
    fmt.Print("请输入你的年龄:")
    fmt.Scanf("%d", &age)
    fmt.Printf("\n%s的年龄是%d岁\n", name, age)
}

输出结果:

请输入你的名字:rex

请输入你的年龄:33

rex的年龄是33岁

英文:

You can replace the last line with this:

fmt.Printf("%s is %d years of age", name, age)

Here is the full code:

package main                                                                                                                            

import "fmt"

func main() {
    var name string
    var age int 
    fmt.Print("Enter your name: ")
    fmt.Scanf("%s", &name)
    fmt.Print("Enter your age: ")
    fmt.Scanf("%d", &age)
    fmt.Printf("\n%s is %d years of age \n", name, age)
}

Output :

> Enter your name: rex
>
> Enter your age: 33
>
> rex is 33 years of age

答案2

得分: 1

请尝试这个代码:

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {

	r := bufio.NewReader(os.Stdin)
	fmt.Print("请输入您的姓名:")
	name, _ := r.ReadString('\n')

	fmt.Print("请输入您的年龄:")
	age, _ := r.ReadString('\n')
	name, age = strings.TrimSpace(name), strings.TrimSpace(age) //去除 \n
	fmt.Println(name, "是", age, "岁")
}

这是一个简单的Go语言代码,它会要求用户输入姓名和年龄,并将其打印出来。

英文:

Try this:

package main

import (
"bufio"
"fmt"
"os"
"strings"
)

func main() {

r := bufio.NewReader(os.Stdin)
fmt.Print("Enter your name: ")
name, _ := r.ReadString('\n')

fmt.Print("Enter your age: ")
age, _ := r.ReadString('\n')
name, age = strings.TrimSpace(name), strings.TrimSpace(age) //remove \n
fmt.Println(name, "is", age, "years of Age")

}

huangapple
  • 本文由 发表于 2014年1月28日 16:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/21400697.html
匿名

发表评论

匿名网友

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

确定