Golang中如何在else语句中循环执行直到满足条件?

Golang中如何在else语句中循环执行直到满足条件? 大家好,我是编程和Go语言的新手。我正在一边阅读《Head First Go》这本书,一边尝试自己构建一个小程序,遇到了一个问题。请帮帮我。

以下是我的原始代码的示例代码。我想重复最后一个 else 块,直到用户输入 y 或 n。我该如何实现这一点?

package main

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

func main() {
	fmt.Println("Wage Calculator")
	repeat := true

	for repeat {
		fmt.Println("get data from user")
		fmt.Println("calculate wage")
		fmt.Println("display wage")
		fmt.Println("Do you want to stop now? (Type y for yes or n for no)")

		reader := bufio.NewReader(os.Stdin)
		input, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(err)
		}
		input = strings.TrimSpace(input)

// Validating user input
		if input == "y" {
			repeat = false
		} else if input == "n" {
			repeat = true	
		} else {
			fmt.Println("Please type either y or n.")
		}
	}
}

感谢您花时间阅读我的帖子。


更多关于Golang中如何在else语句中循环执行直到满足条件?的实战教程也可以访问 https://www.itying.com/category-94-b0.html

4 回复

转念一想,内部循环当然可以表现得更有礼貌一些。

至少在最初几次不成功的迭代中。然后循环可以逐渐开始制造一些动静,直到用户明白为止。

更多关于Golang中如何在else语句中循环执行直到满足条件?的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


你好 @Bangali_Babu

欢迎来到 Go 论坛!

要重复请求正确的输入,你需要在第一个循环内嵌套第二个循环。这个内部循环将反复读取并验证用户的输入。

如果用户输入 y,内部循环可以结束,并让外部循环继续重复。 如果用户输入 n,内部循环可以结束,并让外部循环也结束。 如果用户输入其他内容,内部循环会要求用户就他妈的按 y 或 n,非常感谢,然后重复。

你好,

或者类似这样的代码:

func main() {
	fmt.Println("Wage Calculator")
	repeat := true
	badinput := false

	for repeat {
		if !badinput {
			fmt.Println("get data from user")
			fmt.Println("calculate wage")
			fmt.Println("display wage")
			fmt.Println("Do you want to stop now? (Type y for yes or n for no)")
		} else {
			fmt.Println("Please type either y or n.")
		}

		reader := bufio.NewReader(os.Stdin)
		input, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(err)
		}
		input = strings.TrimSpace(input)

		// Validating user input
		if input == "y" {
			repeat = false
		} else if input == "n" {
			badinput = false
		} else {
			badinput = true
		}
	}
}

要实现else语句的循环直到满足条件,可以使用嵌套循环结构。以下是修改后的代码:

package main

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

func main() {
	fmt.Println("Wage Calculator")
	repeat := true

	for repeat {
		fmt.Println("get data from user")
		fmt.Println("calculate wage")
		fmt.Println("display wage")
		
		// 内层循环处理输入验证
		for {
			fmt.Println("Do you want to stop now? (Type y for yes or n for no)")
			
			reader := bufio.NewReader(os.Stdin)
			input, err := reader.ReadString('\n')
			if err != nil {
				fmt.Println(err)
				continue
			}
			input = strings.TrimSpace(input)
			
			if input == "y" {
				repeat = false
				break
			} else if input == "n" {
				repeat = true
				break
			} else {
				fmt.Println("Please type either y or n.")
				// 这里会继续循环,直到输入y或n
			}
		}
	}
}

或者使用带标签的break语句:

package main

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

func main() {
	fmt.Println("Wage Calculator")
	
outer:
	for {
		fmt.Println("get data from user")
		fmt.Println("calculate wage")
		fmt.Println("display wage")
		
		// 无限循环直到获得有效输入
		for {
			fmt.Println("Do you want to stop now? (Type y for yes or n for no)")
			
			reader := bufio.NewReader(os.Stdin)
			input, err := reader.ReadString('\n')
			if err != nil {
				fmt.Println(err)
				continue
			}
			input = strings.TrimSpace(input)
			
			if input == "y" {
				break outer
			} else if input == "n" {
				break // 只跳出内层循环,继续外层循环
			} else {
				fmt.Println("Please type either y or n.")
			}
		}
	}
}

还可以使用函数封装输入验证逻辑:

package main

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

func getYesNoInput(prompt string) bool {
	reader := bufio.NewReader(os.Stdin)
	
	for {
		fmt.Println(prompt)
		
		input, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(err)
			continue
		}
		
		input = strings.TrimSpace(input)
		
		if input == "y" {
			return true
		} else if input == "n" {
			return false
		} else {
			fmt.Println("Please type either y or n.")
		}
	}
}

func main() {
	fmt.Println("Wage Calculator")
	
	for {
		fmt.Println("get data from user")
		fmt.Println("calculate wage")
		fmt.Println("display wage")
		
		stop := getYesNoInput("Do you want to stop now? (Type y for yes or n for no)")
		if stop {
			break
		}
	}
}
回到顶部