Exercise
Unlimited sum
Objetive
Create a program to allow the user to enter an unlimited amount of numbers. Also, they can enter the following commands:
"sum", to display the sum of all the numbers entered so far.
"view", to display all the numbers entered.
"end", to quit the program.
This is an execution sample:
Number or command? 5
Number or command? 3
Number or command? view
Entered numbers:
5
3
Number or command? 6
Number or command? sum
Sum = 14
Number or command? -7
Number or command? end
Code
Imports System
Imports System.Collections
Namespace Comands
Class Program
Private Shared Sub Main()
Dim [exit] As Boolean = False
Dim answer As String
Dim mylist As ArrayList = New ArrayList()
Dim total As Integer = 0
Do
Console.Write("Number or command? ")
answer = Console.ReadLine()
If answer.ToLower() = "view" Then
Console.WriteLine("Entered numbers: ")
For Each number As Integer In mylist
Console.WriteLine(number)
Next
ElseIf answer.ToLower() = "sum" Then
Console.WriteLine("Sum: {0}", total)
ElseIf answer.ToLower() = "end" Then
[exit] = True
Else
Try
Dim number As Integer = Convert.ToInt32(answer)
mylist.Add(number)
total += number
Catch
Console.WriteLine("Command is not recognized by the system")
End Try
End If
Loop While Not [exit]
End Sub
End Class
End Namespace