如何不允许用户输入超过100的金额

时间:2016-07-10 08:20:50

标签: vb.net

我有这种情况需要用户输入一些数据。我需要的是允许用户输入0到100之间的数字(如果用户输入的数量大于100),而不是显示如下信息:please enter number from 0 to 100然后再次显示他们需要输入该数字的位置

例如,终端中的Console.Write("Español: ")为:

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

如果用户输入的数量超过100,则显示:

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

我正在考虑在下面的代码中执行此操作,使用If ... Else,但是,有更好的方法吗?

以下是实际代码:

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.ReadLine()

        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()
        Else
            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()

        Console.Clear()

   End Select

End Sub

那么,有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我对VB并不熟悉,但试试这个:

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()
Loop

修改3.0 将主题名称添加到数组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

    Do

        If FirstTry = True Then
            Console.WriteLine(subjects(i) & ": ")
            FirstTry = False
        Else
            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

Next

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

Console.ReadLine()
End Sub

答案 1 :(得分:0)

不幸的是,我不知道VBA。但我相信每种语言都是一样的。你可以在循环中做一个{}},同时检查你是否符合条件。在他们遇到之前,你继续从控制台阅读。用你的VBA好看:)