VB:如果变量是其他什么?

时间:2013-09-30 20:43:47

标签: vb.net

我刚刚学习Visual Basic。我试图制作一个计算器,但我想要它,所以如果你在if语句中没有任何其他内容,它会说“对不起”并返回到开头。这是代码:

Module Module1

  Sub Main()
    Dim num1 As Integer
    Dim num2 As Integer
    Dim op As String
    Console.WriteLine("Calculator")
    Console.WriteLine(" ")
    Console.WriteLine("Valid Operators :")
    Console.WriteLine("+(Addition), -(Subtraction), *(Multiplication) & /(Division)")
    Console.WriteLine(" ")
    Console.WriteLine("Enter Number 1 :")
    num1 = Console.ReadLine()
    Console.WriteLine(" ")
    Console.WriteLine("Enter Number 2 :")
    num2 = Console.ReadLine()
    Console.WriteLine(" ")
    Console.WriteLine("Enter Operator :")
    op = Console.ReadLine()
    If op = "+" Then
        Console.WriteLine(" ")
        Console.WriteLine("Answer :")
        Console.WriteLine(num1 + num2)
        Console.WriteLine(" ")
        Console.WriteLine("Press <Enter> To Exit")
        Console.ReadLine()
    End If
    If op = "-" Then
        Console.WriteLine(" ")
        Console.WriteLine("Answer :")
        Console.WriteLine(num1 - num2)
        Console.WriteLine(" ")
        Console.WriteLine("Press <Enter> To Exit")
        Console.ReadLine()
    End If
    If op = "*" Then
        Console.WriteLine(" ")
        Console.WriteLine("Answer :")
        Console.WriteLine(num1 * num2)
        Console.WriteLine(" ")
        Console.WriteLine("Press <Enter> To Exit")
        Console.ReadLine()
    End If
    If op = "/" Then
        Console.WriteLine(" ")
        Console.WriteLine("Answer :")
        Console.WriteLine(num1 / num2)
        Console.WriteLine(" ")
        Console.WriteLine("Press <Enter> To Exit")
        Console.ReadLine()
    End If
    If op = Nothing Then
        Console.WriteLine(" ")
        Console.WriteLine("You have not entered a valid operator.")
        Console.WriteLine(" ")
        Console.WriteLine("Press <Enter> To Exit")
        Console.ReadLine()
    End If

  End Sub

End Module

任何人都可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

您正在寻找一个switch语句(VB中为Select Case)。所有空WriteLine语句也不是必需的。

Module Module1

  Sub Main()
    Dim finished As Boolean = False

    While Not finished

      Dim num1 As Integer
      Dim num2 As Integer
      Dim op As String
      Console.WriteLine("Calculator" & Environment.Newline)
      Console.WriteLine("Valid Operators :")
      Console.WriteLine("+(Addition), -(Subtraction), *(Multiplication) & /(Division)" & Environment.Newline)
      Console.WriteLine("Enter Number 1 :")
      num1 = Console.ReadLine()
      Console.WriteLine(Environment.Newline & "Enter Number 2 :")
      num2 = Console.ReadLine()
      Console.WriteLine(Environment.Newline & "Enter Operator :")
      op = Console.ReadLine()

      Select Case op
        Case "+" 
          Console.WriteLine(Environment.Newline & "Answer :")
          Console.WriteLine((num1 + num2) & Environment.Newline)
          Console.WriteLine("Press <Enter> To Exit")
          Console.ReadLine()
          finished = True

        Case "-" 
          Console.WriteLine(Environment.Newline & "Answer :")
          Console.WriteLine(num1 - num2 & Environment.Newline)
          Console.WriteLine("Press <Enter> To Exit")
          Console.ReadLine()
          finished = True

        Case"*"
          Console.WriteLine(Environment.Newline & "Answer :")
          Console.WriteLine((num1 * num2) & Environment.Newline)
          Console.WriteLine("Press <Enter> To Exit")
          Console.ReadLine()
          finished = True

        Case "/"
          Console.WriteLine(Environment.Newline & "Answer :")
          Console.WriteLine((num1 / num2) & Environment.Newline)
          Console.WriteLine("Press <Enter> To Exit")
          Console.ReadLine()
          finished = True

        Case Is Nothing 
          Console.WriteLine(Environment.Newline & "You have not entered a valid operator." & Environment.Newline)
          Console.WriteLine("Press <Enter> To Exit")
          Console.ReadLine()

        Case Else
          Console.WriteLine(Environment.Newline & "Sorry." & Environment.Newline)
          Console.WriteLine("Press <Enter> To Exit")
          Console.ReadLine() 

      End Select

    End While  

  End Sub

End Module

答案 1 :(得分:0)

您可以这样命名您的行:

Line1:

然后在你的代码中你可以使用它:

Goto Line1

仅供参考,如果我不告诉你,我会失败,不应该这样做,因为这是不好的做法!

答案 2 :(得分:0)

我会在Do-Loop循环中使用Select-case语句解决这个问题,所以在你的代码中,就在你做之前:

Console.WriteLine("Enter Number 1 :")

我会添加一个

Do

最后,在最后一个if语句之前,我会添加

Loop

这样就会创造出“回归开始”的效果

在最后的if语句中我会做

Exit Do

如果符合退出条件。 (执行将转到循环命令后的第一行。)

.
.
.
Do
  Select Case op
  Case "+" 
    Console.WriteLine(" ")
    Console.WriteLine("Answer :")
    Console.WriteLine(num1 + num2)
    Console.WriteLine(" ")
    Console.WriteLine("Press <Enter> To Exit")
    Console.ReadLine()

  Case "-" 
    Console.WriteLine(" ")
    Console.WriteLine("Answer :")
    Console.WriteLine(num1 - num2)
    Console.WriteLine(" ")
    Console.WriteLine("Press <Enter> To Exit")
    Console.ReadLine()

  Case"*"
    Console.WriteLine(" ")
    Console.WriteLine("Answer :")
    Console.WriteLine(num1 * num2)
    Console.WriteLine(" ")
    Console.WriteLine("Press <Enter> To Exit")
    Console.ReadLine()

  Case "/"
    Console.WriteLine(" ")
    Console.WriteLine("Answer :")
    Console.WriteLine(num1 / num2)
    Console.WriteLine(" ")
    Console.WriteLine("Press <Enter> To Exit")
    Console.ReadLine()

  Case Is Nothing 
    Console.WriteLine(" ")
    Console.WriteLine("You have not entered a valid operator.")
    Console.WriteLine(" ")
    Console.WriteLine("Press <Enter> To Exit")
    exit = Console.ReadLine()

    //test here for exit and if exit do:

    Exit Do

  Case Else
    Console.WriteLine("Sorry")
    Console.WriteLine("Press <Enter> To Exit")
    Console.ReadLine() 

End Select

Loop