doudunyi3796 2016-08-25 20:48
浏览 52
已采纳

将值发送到另一个函数golang

I made a random room program with 4 rooms. I'm trying to take the attributes in each room and pass them through to other functions. In each room is a person with a name and an age attribute. I'm trying to pass on those attributes to test against if statements to put out an additional response. How do I pass these values on?

//the random maze room game

package main


//All imports can be combined into ()
import ("fmt"
        //Import needed for random operation
        "math/rand"
        //Import requied to call upon current time 
        "time"
)

type Person struct {
    Name string
    Age int
}



func main(){
    // These two lines are designed to reset the random Seed every time the program is run 
    // Unless the randomizer is seeded, Golang 1.6 is pseudo-random, 
    // always defaulting to Seed(1) 
    s1 := rand.NewSource(time.Now().UnixNano())
    r1 := rand.New(s1)
    switch r1.Intn(4){
    case 0:
        westRoom()
    case 1:
        eastRoom()
    case 2:
        northRoom()
    case 3:
        southRoom()
    default:
        lostRoom()
    }

    p := Person{}
    p.Name = Avatarname
    p.Age = Avatarage
    avatar(&p)
    appearance(&p)  

}

func westRoom(){
    fmt.Println("You find yourself in a room with three walls, and a door behind you.")
    fmt.Println("The opposite wall is a window, overlooking the sea")
    Avatarname := "Bill"
    Avatarage := 25
    return
}

func eastRoom(){
    fmt.Println("You find yourself in a room with a door on the walls to your left, right, and behind you")
    fmt.Println("on the wall across from you is a painting of a mountain scene")
    Avatarname := "Mary"
    Avatarage := 33
    return
}

func northRoom(){
    fmt.Println("You find yourself in a room with a door on the wall behind you")
    fmt.Println("You see several statues of people standing around the room")
    Avatarname := "Joe"
    Avatarage := 58
    return
}

func southRoom(){
    fmt.Println("You find yourself in a room with a door on the wall in front and behind you")
    Avatarname := "Abagail"
    Avatarage := 67
    return
}

func lostRoom(){
    fmt.Println("You are unable to find a room in a maze filled only with rooms")
    fmt.Println("It's almost like the programmer didn't know what he was doing")
}

func avatar(p.Name, p.Age){
    if p.Name == "Bill" || "Joe" {
        fmt.Println("You see a man standing in the middle of the room")
    }   else {
        fmt.Println("You see a woman standing in the middle of the room")
    }
}

func appeareance(p.Name, p.Age) {
    if p.Age > 50 {
        fmt.Println("They look old")
    }   else {
        fmt.Println("They look young")
    }
}
  • 写回答

2条回答 默认 最新

  • doubeng3216 2016-08-25 21:00
    关注

    One way to do this is to change your room functions to return either a Person struct or the attributes of the Person struct.

    Check out the live code here: <kbd>The Go Playground</kbd>

    For example,

    func northRoom() *Person{
      fmt.Println("You find yourself in a room with a door on the wall behind you")
      fmt.Println("You see several statues of people standing around the room")
      return &Person{"Joe", 58}
    }
    

    Now the function calls in your switch statement can expect a Person struct being returned.

    var p *Person
    switch r1.Intn(4){
    case 0:
        p = westRoom()
    case 1:
        p = eastRoom()
    case 2:
        p = northRoom()
    case 3:
        p = southRoom()
    default:
        p = lostRoom()
    }
    
    avatar(p)
    appearance(p) 
    

    Note that you will need to change the signature of your avatar() and appearance() functions to accept a *Person parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度