从扫描仪输入生成新的结构实例。

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

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.
&lt;br/&gt;
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&#39;s values. The program would be in a loop, allowing the user to create multiple instances of the struct. &lt;br/&gt;
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{&quot;gerrard&quot;,&quot;red&quot;,&quot;speed&quot;}
TempCar := Car{&quot;conroy&quot;,&quot;blue&quot;,&quot;cruiser&quot;}

    /* 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 == &quot;car&quot; {
			TempCar.name = Scan1
			TempCar.color = Scan2
			TempCar.design = Scan3
		} else if Scan0 == &quot;boat&quot; {
			TempBoat.name = Scan1
			TempBoat.name = Scan2
			TempBoat.name = Scan3
		} else {
			fmt.Println(&quot;Invalid Input. Try Again.&quot;)
		}
	}
}

}


答案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 (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;strings&quot;
)

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(&quot;Invalid Input. Try Again.&quot;)
		}
		Scan0 := scanMain[0]
		Scan1 := scanMain[1]
		Scan2 := scanMain[2]
		Scan3 := scanMain[3]
		if Scan0 == &quot;car&quot; {
			Cars = append(Cars, Car{Scan1, Scan2, Scan3})
		} else if Scan0 == &quot;boat&quot; {
			Boats = append(Boats, Boat{Scan1, Scan2, Scan3})
		} else {
			fmt.Println(&quot;Invalid Input. Try Again.&quot;)
		}
	}
	fmt.Println(&quot;Boats:&quot;, Boats)
	fmt.Println(&quot;Cars:&quot;, Cars)
}

huangapple
  • 本文由 发表于 2022年10月29日 02:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/74239348.html
匿名

发表评论

匿名网友

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

确定