我的基本计算器出错了

时间:2013-12-23 23:23:28

标签: vb.net

我需要帮助将变量存储为+, - ,/,* ,.我无法弄清楚,而且我还是一个菜鸟。我是通过Vb.net上的控制台应用程序实现的。

Module Module1

Sub Main()
    Do
        Dim UserInput As Object = Nothing
        Dim UserInputNum1 As Object = Nothing
        Dim UserInputNum2 As Object = Nothing
        Dim UserAnswer As Object = Nothing

        Console.WriteLine("Slect Your First Number")
        UserInputNum1 = Console.ReadLine()
        Console.WriteLine("You Slected: " & UserInputNum1)
        Console.ReadLine()

        Console.WriteLine("Slect Your Second Number")
        UserInputNum2 = Console.ReadLine()
        Console.WriteLine("You Slected: " & UserInputNum2)
        Console.ReadLine()

        Console.WriteLine("Would You Like To + - * /")
        UserInput = Console.ReadLine()
        Console.WriteLine("You Slected: " & UserInput)
        Console.ReadLine()

        If UserInput = + Then
            UserAnser = UserInputNum1 + UserInputNum2
        End If

        If UserInput = - Then
            UserAnser = UserInputNum1 - UserInputNum2
        End If

        If UserInput = * Then
            UserAnser = UserInputNum1 * UserInputNum2
        End If

        If UserInput = / Then
            UserAnser = UserInputNum1 / UserInputNum2
        End If

        Console.WriteLine(UserInputNum1 & " " & UserInput & " " & UserInputNum2 & " = " & UserAnswer)
        Console.ReadLine()
    Loop
End Sub

End Module

1 个答案:

答案 0 :(得分:2)

一种选择是使用Console.ReadKey()获取输入,然后使用mSelect Case块来处理它。看看这是否有帮助:

Module Module1

Sub Main()
    Do
        Dim UserInput As Char = Nothing
        Dim UserInputNum1 As Object = Nothing
        Dim UserInputNum2 As Object = Nothing
        Dim UserAnswer As Object = Nothing

        Console.WriteLine("Slect Your First Number")
        UserInputNum1 = Console.ReadLine()
        Console.WriteLine("You Slected: " & UserInputNum1)
        Console.ReadLine()

        Console.WriteLine("Slect Your Second Number")
        UserInputNum2 = Console.ReadLine()
        Console.WriteLine("You Slected: " & UserInputNum2)
        Console.ReadLine()

        Console.WriteLine("Would You Like To + - * /")
        UserInput = Console.ReadKey().KeyChar
        Console.WriteLine(UserInput)
        Console.ReadLine()
        Select Case UserInput
            Case "+"c
                UserAnser = UserInputNum1 + UserInputNum2

            Case "-"c
                UserAnser = UserInputNum1 - UserInputNum2

            Case "*"c
                UserAnser = UserInputNum1 * UserInputNum2

            Case "/"
                UserAnser = UserInputNum1 / UserInputNum2

        Console.WriteLine(UserInputNum1 & " " & UserInput & " " & UserInputNum2 & " = " & UserAnswer)
        Console.ReadLine()
    Loop
End Sub

End Module

字符串文字末尾的c是VB将1个字符串表示为Char的方式