NietzscheProgrammer NietzscheProgrammer - 1 year ago 40 Question

How to don't allow the user to enter an amount of more than 100

I have this case where I need that the user enters some data. All I need is to allow the user to enter numbers from 0 to 100, if the user is entering an amount bigger than 100, than display a message like:

please enter number from 0 to 100
and then show them again where they need to enter that number.

For example,
Console.Write("Español: ")
in the terminal is:

Español: ' the user should enter the number here

if the user enters more than 100, then display this:

Please enter number from 0 to 100. Español: ' here enter the number again

I was thinking on doing this as in the code below, with an If ... Else, but, is there a better way?

Here is the actual code:

Sub Main()
Dim Español1 As Integer
Dim Matematicas1 As Integer
Dim Ciencias1 As Integer
Dim EstudiosSociales1 As Integer
Dim Ingles1 As Integer
Dim ArtesPlasticas1 As Integer
Dim ArtesIndustriales1 As Integer

Select Case Menu

Case 2

Console.Write("Ingrese las notas: ")

Console.Write("Español: ")
' I was thinking on doing this
If Console.ReadLine() >= 100 Then
Console.Write("La nota debe ser 100 o menos: ")
Español1 = Console.ReadLine()
Español1 = Console.ReadLine()
End If

If Español1 = True Then
Console.Write("Matematicas: ")
Matematicas1 = Console.ReadLine()
End If

Console.Write("Ciencias: ")
Ciencias1 = Console.ReadLine()

Console.Write("Estudios Sociales: ")
EstudiosSociales1 = Console.ReadLine()

Console.Write("Ingles: ")
Ingles1 = Console.ReadLine()

Console.Write("Artes plasticas: ")
ArtesPlasticas1 = Console.ReadLine()

Console.Write("Artes Industriales: ")
ArtesIndustriales1 = Console.ReadLine()


End Select

End Sub

So, any suggestions?

Answer Source

I'm not really experienced with VB, but try this:

Dim n as Integer

n = Console.WriteLine()

Do While n >= 100
    Console.WiteLine("Enter new Value:")    'Sorry, no pienso la lengua español :(
    n = Console.ReadLine()

Edit 3.0 Add your subject names into the array subjects.

Sub Main()
Dim subjects As Array = {"Math", "English", "German"} 'Three example names
Dim subjectsInt As New Dictionary(Of String, Integer)
Dim i, input As Integer
Dim check, FirstTry As Boolean

For i = 0 To (subjects.Length - 1)
    check = False
    FirstTry = True


        If FirstTry = True Then
            Console.WriteLine(subjects(i) & ": ")
            FirstTry = False
            Console.WriteLine("Please enter a value between 1 and 100" & " (" & subjects(i) & "):")
        End If

        input = Console.ReadLine()

        If input <= 100 And input > 0 Then
            subjectsInt.Add(subjects(i), input)
            check = True
        End If

    Loop While check = False


For i = 0 To (subjects.Length - 1)
    Console.WriteLine(subjects(i) & ": " & subjectsInt(subjects(i)))

End Sub