-1

我只是在学习 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

任何人都可以帮忙吗?

4

3 回答 3

1

您正在寻找一个 switch 语句(Select Case在 VB 中)。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
于 2013-09-30T21:09:23.010 回答
0

你可以这样命名你的行:

Line1:

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

Goto Line1

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

于 2013-09-30T21:07:09.050 回答
0

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

Console.WriteLine("Enter Number 1 :")

我会添加一个

Do

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

Loop

这样就会产生“回到起点”的效果

在最后一个 if 语句中我会做

Exit Do

如果退出条件已经满足。(执行将转到 Loop 命令之后的第一行)。

.
.
.
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
于 2013-09-30T21:13:14.997 回答