If statement 如果变量为单词

If statement 如果变量为单词,if-statement,basic,If Statement,Basic,对编程非常陌生,我被卡住了。代码还没有100%完成,但应该仍然有效。我在使用if语句时遇到了一个问题,该语句等于一个带单词的变量。我只是添加了引号和它的挂起部分,但是在我添加引号之前,它说如果案例被选中,那么所有的if语句都是真的,并且每个都添加了1。下面是我所拥有的。还有一个与此无关的小问题,如何在句子中间添加一个可变结果?我只能将它们添加到开头或结尾 (节目开始) 也许这是一个更好的方法。如果字符串周围没有引号,那么它会认为它是一个变量。另外,您需要在变量后面加上$,这表示它是一个字符串 W

对编程非常陌生,我被卡住了。代码还没有100%完成,但应该仍然有效。我在使用if语句时遇到了一个问题,该语句等于一个带单词的变量。我只是添加了引号和它的挂起部分,但是在我添加引号之前,它说如果案例被选中,那么所有的if语句都是真的,并且每个都添加了1。下面是我所拥有的。还有一个与此无关的小问题,如何在句子中间添加一个可变结果?我只能将它们添加到开头或结尾

(节目开始)


也许这是一个更好的方法。如果字符串周围没有引号,那么它会认为它是一个变量。另外,您需要在变量后面加上$,这表示它是一个字符串

WesternMarried=0
WesternSingle=0
WesternDivorced=0
WesternSeperated=0
EasternMarried=0
EasternSingle=0
EasternDivorced=0
EasternSeperated=0
SouthernMarried=0
SouthernSingle=0
SouthernDivorced=0
SouthernSeperated=0
MidwesternMarried=0
MidwesternSingle=0
MidwesternDivorced=0
MidwesternSeperated=0
Print "Which state is subject 1 from?"
Input State1$
Print "What is the relationship status of Subject1?"
Input Relation1$
Select Case State1
Case "CA"
    Print "You are from the western region"
    If (Relation1$ = "Married") then
       WesternMarried = WesternMarried + 1
    End If
    If (Relation1$ = "Single") then
       WesternSingle = WesternSingle + 1
    End If
    If (Relation1$ = "Divorced") then
       WesternDivorced = WesternDivorced + 1
    End If
    If (Relation1$ = "Seperated") then
       WesternSeperated = WesternSeperated + 1
    End If
Case Else
    Print "You might be from the midwestern states"
    If (Relation1$ = "Married") then
       MidwesternMarried = MidwesternMarried + 1
    End If
    If (Relation1$ = "Single") then
       MidwesternSingle = MidwesternSingle + 1
    End If
    If (Relation1$ = "Divorced") then
       MidwesternDivorced = MidwesternDivorced + 1
    End If
    If (Relation1$ = "Seperated") then
       MidwesternSeperated = MidwesternSeperated + 1
    End If
End Select
Print "The number of people married in the midwestern states is " ; MidwesternMarried
Print "The number of people single in the midwestern states is " ; MidwesternSingle
Print "The number of people divorced in the midwestern states is " ; MidwesternDivorced
Print "The number of people separated in the Western states is " ; MidwesternSeperated
Print "The number of people married in the Western states is " ; WesternMarried
Print "The number of people single in the Western states is " ; WesternSingle
Print "The number of people divorced in the Western states is " ; WesternDivorced
Print "The number of people separated in the Western states is " ; WesternSeperated
End

更新:当我在标题中看到“basic”时,我以为你指的是VB。我现在已经学会了BASIC实际上是VB基于的语言。。。很抱歉造成混淆。

我相信您可以使用符号“添加”一个变量到字符串中,例如,
打印MidwesternMarried和“人们在中西部州结婚”
。在中间,它会像这样的代码>“字符串文本”和中西部已婚和“更多的文本”<代码> OK测试不出。我仍然对我的If(relation1=“marred”)有问题,如果你知道怎么做的话。在我将“”添加到变量之前,它会使所有的语句都找到状态的情况,但是不管答案如何,它都会发现每个if语句都为true。加上引号,它就无法移动过去。请查看我的答案,看看这是否有帮助。@user3407869我以前对“符号”的评论是不正确的。但是,如果您将我的注释中的符号替换为分号
那么它将是正确的
“字符串文本”;中西部已婚;“更多文本”
似乎很管用,我以前试过,但没用。不知道我以前做错了什么。但我知道它现在起作用了,所以一定是我做了些什么。我改变了If语句,它仍然停留在同一个地方。它不会通过第一个If语句。我已经更新了答案,以解决我怀疑的问题。另外,看看这个网站是的,谢谢。Select Case State1的末尾需要一个$。我一加上这个就行了。非常感谢你的帮助。参加在线编程课程。我想我知道将来我不应该试着把它们带到网上,有时我需要问老师一些事情。但是谢谢你的帮助。我现在就能完成了。我唯一坚持的就是,没问题。是的,我也刚好赶上了火车。它已再次更新。我还意识到VB中的变量后缀在很大程度上似乎与BASIC相同。好的,我保存了这两个站点。希望这能帮助我解决我不懂的事情。再次感谢。
WesternMarried=0
WesternSingle=0
WesternDivorced=0
WesternSeperated=0
EasternMarried=0
EasternSingle=0
EasternDivorced=0
EasternSeperated=0
SouthernMarried=0
SouthernSingle=0
SouthernDivorced=0
SouthernSeperated=0
MidwesternMarried=0
MidwesternSingle=0
MidwesternDivorced=0
MidwesternSeperated=0
Print "Which state is subject 1 from?"
Input State1$
Print "What is the relationship status of Subject1?"
Input Relation1$
Select Case State1
Case "CA"
    Print "You are from the western region"
    If (Relation1$ = "Married") then
       WesternMarried = WesternMarried + 1
    End If
    If (Relation1$ = "Single") then
       WesternSingle = WesternSingle + 1
    End If
    If (Relation1$ = "Divorced") then
       WesternDivorced = WesternDivorced + 1
    End If
    If (Relation1$ = "Seperated") then
       WesternSeperated = WesternSeperated + 1
    End If
Case Else
    Print "You might be from the midwestern states"
    If (Relation1$ = "Married") then
       MidwesternMarried = MidwesternMarried + 1
    End If
    If (Relation1$ = "Single") then
       MidwesternSingle = MidwesternSingle + 1
    End If
    If (Relation1$ = "Divorced") then
       MidwesternDivorced = MidwesternDivorced + 1
    End If
    If (Relation1$ = "Seperated") then
       MidwesternSeperated = MidwesternSeperated + 1
    End If
End Select
Print "The number of people married in the midwestern states is " ; MidwesternMarried
Print "The number of people single in the midwestern states is " ; MidwesternSingle
Print "The number of people divorced in the midwestern states is " ; MidwesternDivorced
Print "The number of people separated in the Western states is " ; MidwesternSeperated
Print "The number of people married in the Western states is " ; WesternMarried
Print "The number of people single in the Western states is " ; WesternSingle
Print "The number of people divorced in the Western states is " ; WesternDivorced
Print "The number of people separated in the Western states is " ; WesternSeperated
End