英文:
Generating new instance of struct from scanner input
问题
我正在尝试运行一个程序,该程序根据用户的输入创建一个预定义结构的新实例。
结构体由3个字符串类型的字段组成。目标是让用户通过扫描仪输入字符串,创建一个新的、唯一的结构体实例,并使用输入作为其值。程序将循环执行,允许用户创建多个结构体实例。
在给出的代码中,有两种结构体类型:Boat和Car。第一个字符串输入将指示选择要创建的结构体类型,接下来的3个字符串将用于填充结构体的值。
package main
import (
"fmt"
"bufio"
"os"
)
type Boat struct {
name string
color string
design string
}
type Car struct {
name string
color string
design string
}
func main() {
TempBoat := Boat{"gerrard", "red", "speed"}
TempCar := Car{"conroy", "blue", "cruiser"}
/* 我使用了一个模板,然后由用户填充,但这只允许一个实例,会不断被覆盖。*/
scanner := bufio.NewScanner(os.Stdin)
for {
if scanner.Scan() {
userIn := scanner.Text()
scanMain := strings.Fields(userIn)
Scan0 := scanMain[0]
Scan1 := scanMain[1]
Scan2 := scanMain[2]
Scan3 := scanMain[3]
if Scan0 == "car" {
TempCar.name = Scan1
TempCar.color = Scan2
TempCar.design = Scan3
} else if Scan0 == "boat" {
TempBoat.name = Scan1
TempBoat.name = Scan2
TempBoat.name = Scan3
} else {
fmt.Println("无效输入。请重试。")
}
}
}
}
<details>
<summary>英文:</summary>
I was attempting to run a program that created a new instance of a predefined struct from the users input.
<br/>
The struct is comprised of 3 fields type string. The goal being for the user to input strings into a scanner, creating a new, unique instance of the struct, using the input as it's values. The program would be in a loop, allowing the user to create multiple instances of the struct. <br/>
In the code presented, there are 2 struct types; Boat and Car. The first string input would indicate which type of struct is chosen to be created, and the next 3 strings would be to fill the struct values.
package main
import(
"fmt"
"bufio"
"os"
)
type Boat struct {
name string
color string
design string
}
type Car struct {
name string
color string
design string
}
func main() {
TempBoat := Boat{"gerrard","red","speed"}
TempCar := Car{"conroy","blue","cruiser"}
/* I was using a template that would then be filled by the user, but
this only allows for one instance that would continue to be
overwritten. */
scanner := bufio.NewScanner(os.Stdin)
for {
if scanner.Scan() {
userIn := scanner.Text()
scanMain := strings.Fields(userIn)
Scan0 := scanMain[0]
Scan1 := scanMain[1]
Scan2 := scanMain[2]
Scan3 := scanMain[3]
if Scan0 == "car" {
TempCar.name = Scan1
TempCar.color = Scan2
TempCar.design = Scan3
} else if Scan0 == "boat" {
TempBoat.name = Scan1
TempBoat.name = Scan2
TempBoat.name = Scan3
} else {
fmt.Println("Invalid Input. Try Again.")
}
}
}
}
答案1
得分: 3
你没有真正指定你想要对读取的值做什么,所以我只是将它们放入一个切片中以供安全保存。
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Boat struct {
name string
color string
design string
}
type Car struct {
name string
color string
design string
}
func main() {
var Boats = []Boat{}
var Cars = []Car{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
userIn := scanner.Text()
scanMain := strings.Fields(userIn)
if len(scanMain) == 0 {
break
}
if len(scanMain) != 4 {
fmt.Println("无效输入。请重试。")
}
Scan0 := scanMain[0]
Scan1 := scanMain[1]
Scan2 := scanMain[2]
Scan3 := scanMain[3]
if Scan0 == "car" {
Cars = append(Cars, Car{Scan1, Scan2, Scan3})
} else if Scan0 == "boat" {
Boats = append(Boats, Boat{Scan1, Scan2, Scan3})
} else {
fmt.Println("无效输入。请重试。")
}
}
fmt.Println("船只:", Boats)
fmt.Println("汽车:", Cars)
}
英文:
You didn't really specify what you wanted to do with the values being read so i just put them into a slice for safe keeping.
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Boat struct {
name string
color string
design string
}
type Car struct {
name string
color string
design string
}
func main() {
var Boats = []Boat{}
var Cars = []Car{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
userIn := scanner.Text()
scanMain := strings.Fields(userIn)
if len(scanMain) == 0 {
break
}
if len(scanMain) != 4 {
fmt.Println("Invalid Input. Try Again.")
}
Scan0 := scanMain[0]
Scan1 := scanMain[1]
Scan2 := scanMain[2]
Scan3 := scanMain[3]
if Scan0 == "car" {
Cars = append(Cars, Car{Scan1, Scan2, Scan3})
} else if Scan0 == "boat" {
Boats = append(Boats, Boat{Scan1, Scan2, Scan3})
} else {
fmt.Println("Invalid Input. Try Again.")
}
}
fmt.Println("Boats:", Boats)
fmt.Println("Cars:", Cars)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论