I'm pretty new in the environment of VB.NET that uses only consoles. I have a problem regarding my program, the console get quickly terminated. How can I my console from doing that? Thanks.
heres my code:
Module Module1
Dim num1 As Integer = 0
Dim num2 As Integer = 0
Dim ans As Decimal = 0
Dim choice As String
Sub Main()
Console.WriteLine("First Number: ")
num1 = Console.ReadLine
Console.WriteLine("Second Number: ")
num2 = Console.ReadLine
Console.WriteLine("[A] Addition")
Console.WriteLine("[S] Subtraction")
Console.WriteLine("[M] Multiplication")
Console.WriteLine("[D] Division")
Console.WriteLine("Enter your choice: ")
choice = Console.ReadLine
If (choice.ToUpper() = "A") Then
ans = num1 + num2
Console.WriteLine(ans.ToString)
ElseIf (choice.ToUpper() = "S") Then
ans = num1 - num2
Console.WriteLine(ans.ToString)
ElseIf (choice.ToUpper() = "M") Then
ans = num1 * num2
Console.WriteLine(ans.ToString)
ElseIf (choice.ToUpper() = "D") Then
ans = num1 / num2
Console.WriteLine(ans.ToString)
End If
End Sub
End Module
-
2Insert Console.ReadKey() at the end just before the End Sub line.03Usr– 03Usr2014年06月13日 12:54:20 +00:00Commented Jun 13, 2014 at 12:54
-
At what point in your code does it get terminated? What does the debugger tell you?Rowland Shaw– Rowland Shaw2014年06月13日 12:54:32 +00:00Commented Jun 13, 2014 at 12:54
-
2Please search how to use Option Strict On. Executing mathematical operations with strings is a really bad thingSteve– Steve2014年06月13日 12:57:09 +00:00Commented Jun 13, 2014 at 12:57
-
Console.ReadKey() pretty works. :) thanks. Even I said im new in the VB.NET environment, people keeps downgrading my question. :/akoDwin– akoDwin2014年06月13日 12:57:31 +00:00Commented Jun 13, 2014 at 12:57
-
3@akoDwin your question may have been downvoted, as you didn't explain how the behaviour differed from what you expected, so not even "it quits immediately after printing the answer" - similarly, there's no demonstration of what you've tried to try and resolve the issue yourselfRowland Shaw– Rowland Shaw2014年06月13日 13:02:33 +00:00Commented Jun 13, 2014 at 13:02
3 Answers 3
Console.ReadKey() should do what you are asking
put it before End Sub
refer here
Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.
Comments
The console program has reached the end of its routine causing it to terminate. You need to put a Console.ReadLine or Console.ReadKey in the main method. This will wait for user input.
I'll usually do something like this
Sub Main()
Console.WriteLine("Press any key to exit")
Console.ReadKey()
Console.WriteLine("Press enter to exit")
Console.ReadLine()
End Sub
Comments
This is normally only an issue when debugging, so I add:
If Debugger.IsAttached Then _
Console.ReadLine()
at the end of my console program Main routines.