The Student Room Group

Computer Sci VB.NET Code Problem solving

Dim Points As Integer
Points = 0
Console.WriteLine("General knowledge quiz time. To answer the question, please type the number beside the answer you think is correct (1,2 or 3).")
Console.WriteLine("Question 1. Which female tennis player recently made the headlines? 1. Maria Sharapova 2. Serena Williams 3.Steffi Graf")
Dim Answer As Integer = Console.ReadLine
If Answer = 1 Then
Console.WriteLine("You're incorrect. You earn 0 points.")
ElseIf Answer = 2 Then
Console.WriteLine("You're correct. You earn 1 point.")
Points = Points + 1
ElseIf Answer = 3 Then
Console.WriteLine("You're incorrect. You earn 0 points.")
Else
Console.WriteLine("You failed to type 1,2 or 3. 0 points have been automatically awarded.")



Console.WriteLine("Question 2. What did Theresa May do lately that maybe she shouldn't have done? 1. Dance 2. Sing 3. Lead the country ")
Answer = Console.ReadLine

If Answer = 1 Then
Console.WriteLine("You're correct. You earn 1 point.")
Points = Points + 1
ElseIf Answer = 2 Then
Console.WriteLine("You're incorrect. You earn 0 points.")

ElseIf Answer = 3 Then
Console.WriteLine("You're incorrect. You earn 0 points.")
Else
Console.WriteLine("You failed to type 1,2 or 3. 0 points have been automatically awarded.")



Console.WriteLine("Question 3. What was the name of the storm that recently caused havoc in North and South Carolina? 1. Storm Frank 2. Storm Florence 3. Storm Felicity")
Answer = Console.ReadLine
If Answer = 1 Then
Console.WriteLine("You're incorrect. You earn 0 points.")
ElseIf Answer = 2 Then
Console.WriteLine("You're correct. You earn 1 point.")
Points = Points + 1
ElseIf Answer = 3 Then
Console.WriteLine("You're incorrect. You earn 0 points.")
Else
Console.WriteLine("You failed to type 1,2 or 3. 0 points have been automatically awarded.")
End If

Console.WriteLine("You've reached the end of the quiz. Your score was.. Drumroll...")
If Points = 3 Then
Console.Write("Amazing! 3/3")
ElseIf Points = 2 Then
Console.Write("Almost.")
Else
Console.Write("You aren't very good.")

End If
Console.ReadKey()
End If
Console.ReadKey()
End If
Console.ReadKey()

First question works, however others don't begin after the user enters the answer. Help? I've fiddled with the indenting but the program defaults it to how it is.
The indenting of your program doesn't affect the way your program works, but when Visual Studio automatically indents for you, it's giving you a huge clue about the flow of the program, which should indicate what's wrong.

Firstly, remember that any set of 'If' 'Else' blocks MUST finish with an 'End If' to get out of that if/else block and carry on to the next part of the program.

Anything in-between your 'Else' and its corresponding 'End If' will be treated as part of the 'Else'. The structure of your code shows that your questions are nested inside each other - they aren't separate - so the only way your second question will be asked will be if the user doesn't choose a valid answer to the first question.

Secondly, since indenting makes no difference, that means these two snippets of code do exactly the same thing, even though they're indented differently (VB.NET isn't Python)
1 - look at what happens when x is not 3:



If x = 3 Then
Console.WriteLine("x is 3")
Else
Console.WriteLine("x is not 3")
Console.WriteLine("Another Line")



2 - behaves in exactly the same way as the above example when x is not 3:



If x = 3 Then
Console.WriteLine("x is 3")
Else
Console.WriteLine("x is not 3")

Console.WriteLine("Another Line")



Neither of these have an 'End If' - but the 'End If' is what really makes a difference. Again, indentation is irrelevant: In both cases the Console.WriteLine("Another Line") is inside the final Else statement, even though the formatting of the code suggests otherwise - don't let formatting deceive you, instead look at Visual Studio's formatting and let it highlight when there's a problem




If x = 3 Then
Console.WriteLine("x is 3")
Else
Console.WriteLine("x is not 3")
End If

Console.WriteLine("Another Line")



By doing this, the final Console.WriteLine is now completely outside of the If/Else structure.

Another way to find problems like this in your code is to learn how to use the Visual Studio debugger. This is a feature of Visual Studio which will let you see exactly what your code is doing line-by-line - you just set a breakpoint in the left-hand-side margin of the text editor, then use 'F10' or 'F11' to step through / step into your code. You can also mouse-over variables while the program is running to see what their data is. I strongly recommend spending a few minutes learning about this, because it'll save you hours when you run into logic problems in your program:
https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017
(Don't worry that it uses the C# language to demonstrate - it's exactly the same in VB.NET)
(edited 5 years ago)

Quick Reply

Latest