-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

Select Caseswitch ステートメント ( 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 に答える