具有多个条件的if语句 [英] if statements with multiple conditions

查看:70
本文介绍了具有多个条件的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去一周来,我一直在到处寻找信息.我有一个小的 console应用程序,它会向用户询问一系列问题并将答案存储在变量中.我想做的是将这些答案与一系列条件进行比较(在这种情况下为焊接过程),然后选择与所有条件匹配的过程.我尝试使用if和语句执行此操作,但是程序仅使用我的第一个If语句,并且不尝试进行任何比较...显然我做错了什么..这是我的代码:

  Dim r作为字符串调暗为字符串昏暗x两倍昏暗的两倍Dim z As String昏暗v作为字符串暗淡如弦Dim b作为字符串昏暗的我双重Console.WriteLine(这是管道还是设施?")t = Console.ReadLine()Console.WriteLine(这是修复程序吗?")b = Console.ReadLine()Console.WriteLine(这是CSA还是ASME?")r = Console.ReadLine()Console.WriteLine(已向BCSA或ABSA注册吗?")一个= Console.ReadLine()如果a ="然后a ="bcsa"万一Console.WriteLine(请输入管道尺寸")x = Console.ReadLine()Console.WriteLine(请输入壁厚")y = Console.ReadLine()Console.WriteLine(等级是多少?")z = Console.ReadLine()如果r ="ASME",则Console.WriteLine(请输入物料组e.x:组1、2、3 ..")v = Console.ReadLine()别的v = 1000万一Console.WriteLine(请输入冲击温度(请输入数值)")我= Console.ReadLine()如果i ="那么i ="0"万一如果t =设施"且r ="asme"且a ="bcsa"&x< = 100&x>0&y< = 25.4&y> = 1.5748&z ="p1"&v> = 1&v< = 3&我> = -40然后Console.WriteLine(我建议焊接程序MII-13-FAB11 Rev.1_BCSA")Console.WriteLine(您要打开此文件吗?")如果Console.ReadLine()="yes",则Dim yes As String ="Q:\ Macro Database \ Use \ MII-13-FAB11 Rev.1_BCSA Reg..pdf"Process.Start(是)ElseIf Console.ReadLine()="no"然后Console.WriteLine(还不错,谢谢您使用Citrus WPS选择工具.")万一万一'MII-13-FAB11 Rev.0_ABSA如果t =设施"&r ="asme"&a ="absa"&x< = 100&x>0&y< = 25.4&y> = 1.5748&z ="p1"&v> = 1&v< = 3&我> = -40然后Console.WriteLine(我建议焊接程序MII-13-FAB11 Rev.0_ABSA")Console.WriteLine(您要打开此文件吗?")如果Console.ReadLine()="yes",则Dim yes As As String ="Q:\ Macro Database \ Use \ MII-13-FAB11 Rev.0_ABSA Reg..pdf"Process.Start(是)ElseIf Console.ReadLine()="no"然后Console.WriteLine(还不错,谢谢您使用Citrus WPS选择工具.")万一万一'MII-10-PL4 Rev.1如果t =管道"&b =否"&r ="csa"&a ="bcsa"&x< = 323.9&x>0&y< = 12.84&y> = 1.5&z< = 386&我> = -20然后Console.WriteLine(我建议焊接程序MII-10-PL4 Rev.1")Console.WriteLine(您要打开此文件吗?")如果Console.ReadLine()="yes",则Dim yes As As String ="Q:\ Macro Database \ Use \ MII-10-PL4 Rev.1.pdf"Process.Start(是)ElseIf Console.ReadLine()="no"然后Console.WriteLine(还不错,谢谢您使用Citrus WPS选择工具.")万一万一 

我只希望包括三个过程,以便能给出要点.所以在"If"语句上,我尝试使用" And","ElseOr",&","Or".当我进入并在Visual Studio中运行此代码时,它会自动默认为完全焊接优先程序.我查看了 Select Case 块,我不确定如何对这些信息进行排序?任何帮助深表感谢!我真的不太确定自己在做的事情是否部分正确!

谢谢大家!

解决方案

当您应该使用 And (或 AndAlso )

在VB.NET中:-

& 运算符生成两个表达式的字符串连接."

And 运算符对两个布尔表达式执行逻辑连接,或对两个数值表达式执行按位连接"

我怀疑您实际上是想使用 AndAlso ,它与 And 相同,但是如果其中一个结果为false,则将布尔表达式短路为False

I have been looking everywhere for the past week trying to figure this out. I have a small console application which asks the user a series of questions and stores the answers in variables. What I would like it to do is compare these answers against a series of conditions (welding procedures in this case) then select the procedure that matches all of the conditions. I tried doing this using if and statements but the program only uses my first If statement and does not try to compare anything... Clearly I am doing something quite wrong.. Here's my code:

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

I only included three procedures hopefully that gives the gist of it. so on the "If" statements I tried to use "And", "ElseOr", "&", "Or".. When I step into and run through this code in visual studio it Automatically defaults to the very weld first procedure. I looked at the Select Case blocks I am unsure how to sort this information with them? Any help is much appreciated! I'm really not too sure if what I am doing is even partially correct!

Thanks Everyone!

解决方案

You are using & when you should be using And (or AndAlso)

In VB.NET:-

The & Operator "Generates a string concatenation of two expressions."

The And operator "Performs a logical conjunction on two Boolean expressions, or a bitwise conjunction on two numeric expressions"

I suspect that you actually want to use AndAlso which does the same as And but short-circuits the boolean expression to False if one of the results is false

这篇关于具有多个条件的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆