具有多个条件的 if 语句
Posted
技术标签:
【中文标题】具有多个条件的 if 语句【英文标题】:if statements with multiple conditions 【发布时间】:2014-10-20 10:25:00 【问题描述】:在过去的一周里,我一直在四处寻找,试图弄清楚这一点。我有一个小的console application
,它会询问用户一系列问题并将答案存储在变量中。我想做的是将这些答案与一系列条件(在这种情况下为焊接程序)进行比较,然后选择与所有条件匹配的程序。我尝试使用 if 和语句执行此操作,但程序仅使用我的第一个 If 语句并且不尝试比较任何内容...显然我做错了..这是我的代码:
Dim r As String
Dim a As String
Dim x As Double
Dim y As Double
Dim z As String
Dim v As String
Dim t As String
Dim b As String
Dim i As Double
Console.WriteLine("Is this for pipeline or facility?")
t = Console.ReadLine()
Console.WriteLine("Is this a repair procedure?")
b = Console.ReadLine()
Console.WriteLine("Is this CSA or ASME?")
r = Console.ReadLine()
Console.WriteLine("Registered with BCSA or ABSA?")
a = Console.ReadLine()
If a = "" Then
a = "bcsa"
End If
Console.WriteLine("Please Enter a Pipe Size")
x = Console.ReadLine()
Console.WriteLine("Please Enter a Wall Thickness")
y = Console.ReadLine()
Console.WriteLine("What is the Grade?")
z = Console.ReadLine()
If r = "ASME" Then
Console.WriteLine("Please Enter the Material Group e.x: Group 1, 2, 3..")
v = Console.ReadLine()
Else
v = 1000
End If
Console.WriteLine("Please enter an Impact Temperature (numerical values only please)")
i = Console.ReadLine()
If i = "" Then
i = "0"
End If
If t = "facility" And r = "asme" And a = "bcsa" & x <= 100 & x > 0 & y <= 25.4 & y >= 1.5748 & z = "p1" & v >= 1 & v <= 3 & i >= -40 Then
Console.WriteLine("I suggest the Weld Procedure MII-13-FAB11 Rev.1_BCSA")
Console.WriteLine("Would you like to open this file?")
If Console.ReadLine() = "yes" Then
Dim yes As String = "Q:\Macro Database\Use\MII-13-FAB11 Rev.1_BCSA Reg..pdf"
Process.Start(yes)
ElseIf Console.ReadLine() = "no" Then
Console.WriteLine("Okay fair enough. Thank you for using Citrus WPS Selection tool.")
End If
End If
'MII-13-FAB11 Rev.0_ABSA
If t = "facility" & r = "asme" & a = "absa" & x <= 100 & x > 0 & y <= 25.4 & y >= 1.5748 & z = "p1" & v >= 1 & v <= 3 & i >= -40 Then
Console.WriteLine("I suggest the Weld Procedure MII-13-FAB11 Rev.0_ABSA")
Console.WriteLine("Would you like to open this file?")
If Console.ReadLine() = "yes" Then
Dim yes As String = "Q:\Macro Database\Use\MII-13-FAB11 Rev.0_ABSA Reg..pdf"
Process.Start(yes)
ElseIf Console.ReadLine() = "no" Then
Console.WriteLine("Okay fair enough. Thank you for using Citrus WPS Selection tool.")
End If
End If
' MII-10-PL4 Rev.1
If t = "pipeline" & b = "no" & r = "csa" & a = "bcsa" & x <= 323.9 & x > 0 & y <= 12.84 & y >= 1.5 & z <= 386 & i >= -20 Then
Console.WriteLine("I suggest the Weld Procedure MII-10-PL4 Rev.1")
Console.WriteLine("Would you like to open this file?")
If Console.ReadLine() = "yes" Then
Dim yes As String = "Q:\Macro Database\Use\MII-10-PL4 Rev.1.pdf"
Process.Start(yes)
ElseIf Console.ReadLine() = "no" Then
Console.WriteLine("Okay fair enough. Thank you for using Citrus WPS Selection tool.")
End If
End If
我只包括了三个程序,希望能给出它的要点。所以在“If”语句中,我尝试使用"And", "ElseOr", "&", "Or"..
当我在 Visual Studio 中进入并运行此代码时,它会自动默认为焊接优先程序。我查看了Select Case
块我不确定如何使用它们对这些信息进行排序?任何帮助深表感谢!我真的不太确定我所做的是否部分正确!
谢谢大家!
【问题讨论】:
好的,我刚刚将变量中的所有“&”改回“And”,还将变量中的“double”改回“string”,它似乎突然起作用了!在 If 语句中可以使用多少个“与”有限制吗? 您可以根据需要使用任意数量的And's, Or's
。
另请注意,每个变量允许多个字符。说真的,你会在 6 个月内理解这段代码吗?
@MattWilko 您对&
的看法是正确的。我以为c#也是一样。我的错。
【参考方案1】:
当你应该使用And
(或AndAlso
)时,你正在使用&
在 VB.NET 中:-
&
运算符“生成两个表达式的字符串连接。”
And
运算符“对两个布尔表达式执行逻辑连接,或对两个数值表达式执行按位连接”
我怀疑您实际上想要使用 AndAlso
,它与 And
的作用相同,但如果其中一个结果为 false,则将布尔表达式短路为 False
【讨论】:
以上是关于具有多个条件的 if 语句的主要内容,如果未能解决你的问题,请参考以下文章