Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 2008 在Visual Basic 2008中使用IF运算符_Visual Studio 2008 - Fatal编程技术网

Visual studio 2008 在Visual Basic 2008中使用IF运算符

Visual studio 2008 在Visual Basic 2008中使用IF运算符,visual-studio-2008,Visual Studio 2008,我可以使用IF-THEN-ELSE结构在VisualBasic2008中编写以下问题。但我想知道如何在VisualBasic2008中使用IF操作符对其进行编码 编写一个程序,要求输入3个数字,并显示两个最高数字的总和 除了标准答案(“拿起一本像样的通用编程书”):如果任务是按照您所说的那样陈述的——也就是说,没有限制或指导原则——您根本不需要IFs。可以使用LINQ在一行中完成: Dim numbers As List(Of Integer) numbers = New List(Of

我可以使用IF-THEN-ELSE结构在VisualBasic2008中编写以下问题。但我想知道如何在VisualBasic2008中使用IF操作符对其进行编码

编写一个程序,要求输入3个数字,并显示两个最高数字的总和

除了标准答案(“拿起一本像样的通用编程书”):如果任务是按照您所说的那样陈述的——也就是说,没有限制或指导原则——您根本不需要IFs。可以使用LINQ在一行中完成:

Dim numbers As List(Of Integer)
    numbers = New List(Of Integer)

    numbers.Add(9)
    numbers.Add(-4)
    numbers.Add(1)

    Console.WriteLine(numbers.OrderByDescending(Function(n) n).Take(2).Sum())
例如,可以使用for循环(从1到3)读入数字

编辑如下

如果你要特别使用If语句,而不是ELSE语句(我就是这样理解你的问题/评论的),那基本上是一个一般的计算机科学练习,而不是编程问题;)但是无论如何(我将使用C语法,因为我更容易用它来写,对不起):

你有3个数字,(x,y,z)。使用关系>=(大于或等于),它们可以按6(3!)种阶乘方式排列:

现在,不幸的是,在大多数语言中,您不能使用以下语句:

if(x >= y >= z)
但是你可以把它分成两部分

if(x >= y && y >= z)
这为您提供了一种在每个可能的场景中仅执行特定代码块的方法:

if(x >= y && y >= z){
    // biggest numbers: x, y
    return x + y;
}
if(x >= z && z >= y){
...and so on, 6 times total

请注意,这是一种幼稚的方法,不是最好的解决方案。如果你赶时间,就行了。如果您有空闲时间,您可以考虑更优化的解决方案-作为练习;)

如果你真的必须使用
如果
,你可以修改@David或@Gerino的想法:

    Dim inputs(2) As Integer

    For index = 0 To 2
        inputs(index) = Int32.Parse(Console.ReadLine)
    Next

    If True Then
        Console.Write(inputs.OrderByDescending(Function(f) f).Take(2).Sum())
    End If

    Console.ReadLine()
或者,如果您想演示短路
if
语法的知识:

    Dim inputs(2) As Integer

    For index = 0 To 2
        inputs(index) = Int32.Parse(Console.ReadLine)
    Next

    Dim totalSum As Integer = inputs.OrderByDescending(Function(f) f).Take(2).Sum()
    Dim output As String = If(totalSum > 0, "Positive: " & totalSum, "Negative:" & totalSum)

    Console.Write(output)
    Console.ReadLine()

听起来根本不需要
If
。请求三个输入,将它们存储在集合中,按升序排列集合,跳过1个元素,对其余元素求和,显示总和。但我想知道如何使用IF运算符,而不是IF-THEN-ELSE结构。请向老师寻求指导或提示,或者显示您在IF-THEN-ELSE结构中尝试过的内容,以及在IF结构中不起作用的内容。由于我是Visual Basic新手,我的老师要求使用IF运算符。我该怎么做?请帮助我,我正在寻找以下类型。如果([argument1,]argument2,argument3)
    Dim inputs(2) As Integer

    For index = 0 To 2
        inputs(index) = Int32.Parse(Console.ReadLine)
    Next

    Dim totalSum As Integer = inputs.OrderByDescending(Function(f) f).Take(2).Sum()
    Dim output As String = If(totalSum > 0, "Positive: " & totalSum, "Negative:" & totalSum)

    Console.Write(output)
    Console.ReadLine()