如何在尝试使用VB中的DO..UNTIL LOOP输入密码3次后限制用户 [英] How do I restrict a user after 3 attempts trying to enter the password using DO..UNTIL LOOP in VB

查看:141
本文介绍了如何在尝试使用VB中的DO..UNTIL LOOP输入密码3次后限制用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个程序,允许用户最多3次尝试输入密码secret。如果用户在3次尝试中正确输入密码,则会显示密码有效的消息。否则,程序将显示密码不正确的消息。



使用DO ... UNTIL LOOP with IF..THEN



这一定必须使用DO ..UNTIL LOOP。请不要......如果。

教师要求使用DO ... UNTIL LOOP寻找解决方案

这是学校作业。



我尝试了什么:



我能够通过IF..THEN做到这一点但是我不能用DO..UNTIL LOOP来做。

请紧急。

这是我用过的编码,但它无法正常工作。

请给我正确答案。

Create a program that allows the user up to 3 attempts to enter the password 'secret'. If the user enters the password correctly in the 3 attempts, a message is displayed that the password is valid. Otherwise, the program displays the message that the password is incorrect.

USING DO...UNTIL LOOP with IF..THEN

THIS MUST BE DONE USING DO..UNTIL LOOP. PLEASE NO IF..THEN.
THE TEACHER ASKED TO FIND THE SOLUTION USING DO..UNTIL LOOP
THIS IS FOR A SCHOOL ASSIGNMENT.

What I have tried:

I was able to do it through just IF..THEN but I cant do it using DO..UNTIL LOOP.
Please its urgent.
This the coding that I have used, but it doesn't work correctly.
Please give me the correct answer.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pass, word As String
        Dim num As Integer
        num = 1
        pass = "secret"
        word = TextBox1.Text
        'If word = pass Then
        '    MsgBox("VALID")
        'Else
        '    MsgBox("INVALID")
        'End If
        'num = num + 1
        'Do
        '    num = num + 1
        'Loop Until num = 3
        'MsgBox("RUN")
        Do
            If num >= 3 Then
                MsgBox("ATTEMPTS EXCEEDED")
            End If
            If word = pass And num <= 3 Then
                MsgBox("VALID")
            End If
            'Do
            '    num += 1
            'Loop Until num > 3
            'If num >= 3 Then
            '    MsgBox("ATTEMPTS EXCEEDED")
            'End If
            'If word <> pass Then
            '    MsgBox("INVALID")
            'End If
            num += 1
            If word <> pass Then
                MsgBox("INVALID")
            End If
        Loop Until num > 3
    End Sub
End Class

推荐答案

除非你正在进行其他计算,否则你不需要Do循环。



在你的Form或UserControl中,登录按钮是present add;



You dont need a Do Loop for this unless you are doing other calculations.

At your Form or UserControl, where the Button of Login is present add;

Dim attempts As Integer = 0

Dim userName As String = "user123"
Dim password As String = "12345"

Private Sub btLogin_Click(sender As Object, e As EventArgs) Handles btLogin_Click

If attempts >=3 Then
'maximal attempts reached
MsgBox("You reached the maximum attempts to log in!")
Exit Sub
End If


If (textBoxUserName.Text = userName) AndAlso (textBoxPasword.Text = password) Then
'Correct username and password

   'add code to procede

Else
'Incorrect username or password
attempts +=1
End if


End Sub


尝试这样的事情:

Try something like this:
Dim attempts As Integer
attempts = 0
Do
    attempts = attempts + 1
    If password = "secret" Then
        MsgBox("VALID")
        Exit Do
    Else
        MsgBox("Invalid")
    End If
Loop Until attempts > 2


这里是一个没有任何if-thens的控制台应用程序。



请记住,您可以在循环出口处放置多个条件。



Here it is as a console app without any if-thens.

Remember you can put multiple conditions on the loop exit.

Dim pwTries As Integer = 0
Const secretPassword = "secret"
Dim pwMatch As Boolean = False
Do Until pwMatch Or pwTries > 2
    Console.WriteLine("Enter you password.")
    pwMatch = (Console.ReadLine = secretPassword)
    pwTries += 1
Loop
Console.WriteLine("Password is " & If(pwMatch, "valid", "incorrect") & ".")
Console.WriteLine("Click any key to continue.")
Console.ReadKey()


这篇关于如何在尝试使用VB中的DO..UNTIL LOOP输入密码3次后限制用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆