【问题标题】:Visual Basic Do While Loop with several OR's带有多个 OR 的 Visual Basic Do While 循环
【发布时间】: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


【解决方案1】:

尝试使用“and”而不是“or”。

【讨论】:

  • 符合条件时也会接受。显然这个回答太早了。 9 分钟内说完
  • 我想 9 分钟到了?嗯?
  • 抱歉花了这么长时间,让我忘记了(所有 9 个月的时间……从那以后有了双胞胎)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 2020-12-30
  • 2021-03-08
  • 2021-01-31
  • 1970-01-01
相关资源
最近更新 更多