-
Aug 12th, 2013, 03:05 PM
#1
Thread Starter
New Member
Need help with a loop in vb.net console
I am really new to VB and have a question about loops. I am programming a console based text adventure and need to know how to make the wrong choice loop back to the selection. By wrong, I mean something that's not even a choice. Like below, when someone puts in '3'. I want it to say ("What is so hard about '1' or '2'. Pick a number between 1 and 2....") and then repeat the question with the choices. Any help is appreciated.
Console.WriteLine("Do you want to quit or get started? Type the corresponding number")
Console.WriteLine("and press ENTER to submit your choice.")
Console.WriteLine()
Console.WriteLine("1. Get Started!")
Console.WriteLine("2. No Thanks!")
Begin = Console.ReadLine()
If Begin = 1 Then
Console.WriteLine("Let's get started!")
Console.ReadKey()
ElseIf Begin = 2 Then
Console.WriteLine("See ya next time!")
Console.ReadKey()
Else
Console.WriteLine("What is so hard about '1' or '2'. Pick a number between 1 and 2....")
Console.ReadLine()
End If
PS. Sorry for no indentions. kinda new to the forum thing, too.
-
Aug 12th, 2013, 03:12 PM
#2
Re: Need help with a loop in vb.net console
Hi Kadoza, welcome to the forum! Please, whenever you post code, post them in code tags:
[code]
'Code here
[/code]
Try something like this:
Code:
Sub Main()
Call Start
End Sub
Private Sub Start()
Console.WriteLine("Do you want to start your adventure? 1 - Yes or 2 - No")
Dim Begin As String
Begin = Console.ReadLine()
If Begin = "1" Then
Console.WriteLine("Let's get started!")
'Call your next sub here
ElseIf Begin = "2" Then
Console.WriteLine("See ya next time!")
Console.ReadKey()
Else
Console.WriteLine("Invalid entry.")
Call Start()
End If
End Sub
Edit - I actually made a console app slots game a while back and recently updated it. The reason I mention this is because it's exactly what you need even though it's a different type of game(mine's casino where as yours is rpg). Check it out here.
Last edited by dday9; Aug 12th, 2013 at 03:52 PM.
-
Aug 12th, 2013, 03:13 PM
#3
Frenzied Member
Re: Need help with a loop in vb.net console
You can look into the Do/While conditional loop. That should give you what you're wanting.
-
Aug 12th, 2013, 04:07 PM
#4
Re: Need help with a loop in vb.net console
First off, I totally support the sarcastic response you are intending for an out of range value.
Since you are starting out, do yourself a favor and turn Option Strict ON for the project. That can be found on the Compile tab of Propject|Properties. What this will do will be to force you to use proper type conversions rather than relying on the slow (and occasionally wrong) implicit type conversions. For example, ReadLine will return a string, but Begin is clearly an integer. The string should be properly converted to an integer using Integer.TryParse.
Next, I think that do loop would probably work fairly well here. It would look something like this:
Code:
Do
If integer.TryParse(console.ReadLine,Begin) Then
'It was an integer, and is now in Begin.
Else
'It wasn't an integer, so Begin will hold 0.
End If
If Begin < 1 OrElse Begin > 2 Then
Begin = 0
'Scold the user
End If
Loop While Begin = 0
That code is a bit overwrought, since there really isn't a use for the Else and not really a use for the If, either, but I thought an example of what you can do with Integer.TryParse might be in order.
My usual boring signature: Nothing
-
Aug 12th, 2013, 05:04 PM
#5
Thread Starter
New Member
Re: Need help with a loop in vb.net console
Thanks. Got the loop working now. Just a one more question.
What are the commands for clearing the console and exiting the console. And any other commands that may be useful.
-
Aug 12th, 2013, 05:10 PM
#6
Re: Need help with a loop in vb.net console
Tale a look at the MSDN documentation here.
-
Aug 12th, 2013, 05:26 PM
#7
Fanatic Member
Re: Need help with a loop in vb.net console
If I were you, I would use a GoTo.
-
Aug 12th, 2013, 05:30 PM
#8
Re: Need help with a loop in vb.net console
Originally Posted by Flashbond
If I were you, I would use a GoTo.
:O
Please tell me that was a sick joke meant for the VB6 forum?
-
Aug 13th, 2013, 04:11 AM
#9
Fanatic Member
Re: Need help with a loop in vb.net console
In fact no, I still do use it if I can't manage so many nested loops
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|