【发布时间】:2014-02-15 22:55:00
【问题描述】:
好的,所以我是 Visual Basic 的新手,而不是其他人。我有一个大学作业,这可能是超前的,但我决定在我们为作业编写的程序中进行一些自动错误检查。我们还没有讨论循环,但是我的“不要相信用户不输入他们不应该输入的东西”的感觉非常痒。这是我的代码:
Console.WriteLine("What is your marital status?")
Console.Write("Enter (m)arried, (s)ingle, (d)ivorced, (w)idowed: ")
maritalStatus = Console.ReadLine()
Do While maritalStatus IsNot "m" Or maritalStatus IsNot "s" Or maritalStatus IsNot "d" Or maritalStatus IsNot "w"
Console.WriteLine("Please enter a valid marital status.")
Console.Write("Enter (m)arried, (s)ingle, (d)ivorced, (w)idowed: ")
maritalStatus = Console.ReadLine()
Loop
Console.Clear()
好消息是,如果您输入除 m、s、d 或 w 以外的任何内容,它会循环返回并告诉您输入有效的内容。坏消息是,即使您输入 m、s、d 或 w,它也会这样做。
我在谷歌上搜索的耐心越来越少。有什么帮助吗?
【问题讨论】:
-
尝试“和”而不是“或”。
-
顺便说一下,将 IsNot 更改为 运算符也不行。
-
@BrianKnoblauch 这行得通。我想我已经工作太久了,睡眠太少了......把它作为答案,我会这样标记它
-
所以如果不是
m那么它将循环。如果不是s,那么它将循环。如果不是d,那么它将循环。如果不是w,那么它会循环...... -
不同的编程语言,但是similar problem
标签: while-loop console-application visual-studio-2013 do-while do-loops