doufan6544 2014-01-28 08:45
浏览 41

我可以使用Go语言输入值的语法

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("
Enter 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
  • 写回答

2条回答 默认 最新

  • drzil26260 2014-01-28 08:57
    关注

    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("
    %s is %d years of age 
    ", name, age)
    }
    

    Output :

    Enter your name: rex

    Enter your age: 33

    rex is 33 years of age

    评论

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染