Calculator - switch Learn programming Visual Basic (VB.net)

Lesson:

Basic Data Types


Exercise:

Calculator - switch


Objetive:

Write a Visual Basic (VB.net) program that asks the user for two numbers and an operation to perform on them (+,-,*,x,/) and displays the result of that operation, as in this example:

Enter the first number: 5
Enter the operation: +
Enter the second number: 7
5+7=12

Note: You MUST use 'switch', not 'if'.


Code:

Imports System
Public Class exercise054
    Public Shared Sub Main()
        Dim a, b As Integer
        Dim operation As Char
        Console.Write("Enter first number: ")
        a = Convert.ToInt32(Console.ReadLine())
        Console.Write("Enter operation: ")
        operation = Convert.ToChar(Console.ReadLine())
        Console.Write("Enter second number: ")
        b = Convert.ToInt32(Console.ReadLine())

        Select Case operation
            Case "+"c
                Console.WriteLine("{0} + {1} = {2}", a, b, a + b)
            Case "-"c
                Console.WriteLine("{0} - {1} = {2}", a, b, a - b)
            Case "x"c, "*"c
                Console.WriteLine("{0} * {1} = {2}", a, b, a * b)
            Case "/"c
                Console.WriteLine("{0} / {1} = {2}", a, b, a / b)
            Case Else
                Console.WriteLine("Wrong Character")
        End Select
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.