【问题标题】:Regarding the repetion of block for limited times关于限时重复块
【发布时间】:2019-12-12 03:00:49
【问题描述】:

我实现了一个随机函数来生成随机 OTP,我希望用户最多输入错误的 OTP 3 次。如果用户连续第四次尝试输入正确的 OTP 失败,则必须终止循环。我无法解决它

我已经在 Visual Studio 中尝试过这个,并且遇到了这个连续循环的问题。

string otps = otp.getOtp(); // Get Random otp from getotp method below
Console.WriteLine("OTP Generated:{0}", otps);
do
{
    Console.WriteLine("Enter OTP");
    string userotp = Console.ReadLine(); // Read OTP
    if (userotp == otps) // If OTP is valid
    {
        val1 = false;
        return totalprice;
    }
    else
    {
        while (i >= 1)
        {
            Console.WriteLine("Incorrect OTP");                                          
            Console.WriteLine("Please Re Enter your Password {0} attempts left", i);
            if(userotp != otps)
            {
                val1 = true;
                i--;
            }                                            
            else if (userotp == otps) // If OTP is valid
            {
                val1 = false;
                return totalprice;
            }
        }
     }
 } while (val1);

**************************************  WELCOME TO WALMART  ************************************************
Enter Product Name:
sgf
Enter Product Price:
4356
-----------------------------
 Total price :4356
 -----------------------------
Please Enter Payment option:
 1.CreditCard
 2.NetBanking
 3.Paytm
1
Enter Credit Card Number:
23456789
ReEnter Credit Card Number:
23456789
Enter your Name
Chakradhar
Please Enter CVV Number
***OTP Generated:444
Enter OTP
555
Incorrect OTP
Please Re Enter your Password 3 attempts left
//I NEED TO CALL THE ENTER OTP HERE//
Incorrect OTP
Please Re-Enter your Password 2 attempts left
Incorrect OTP
Please Re-Enter your Password 1 attempts left
Enter OTP

【问题讨论】:

  • 什么是一次性密码?为什么我需要知道 OTP 是什么才能理解您的问题?你真正的问题是什么?请改进您的帖子。阅读How to Ask,了解如何以清晰、可回答的方式提出您的问题。确保您还阅读了minimal reproducible example,这样您就知道要发布什么样的代码。准确解释该代码的作用,您希望它做什么,以及准确地您需要帮助弄清楚它是什么。

标签: c# loops one-time-password


【解决方案1】:

需要注意的一点是,将val1 设置为终止循环并返回是没有意义的。你把这个简单的问题复杂化了。我会建议一个更简单的方法:

string otp=otp.getOtp();
string input;
int attemptsLeft=3;

while(attemptsLeft>0)
{
   Console.WriteLine("Enter OTP");
   input=Console.ReadLine();
   if(input==otp)
       return totalprice;
   else
   {
        Console.WriteLine("Incorrect OTP");  
        Console.WriteLine("Please Re Enter your Password {0} attempts left", attemptsLeft--);
   }

}
//Handle three unsuccessful attempts

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2017-10-02
    • 2018-04-10
    • 1970-01-01
    • 2011-10-21
    相关资源
    最近更新 更多