仅在输入PIN时如何将PIN输入4位数字并进行验证?

时间:2018-11-19 06:20:33

标签: c#

我不熟悉代码,因为我是C#的初学者,所以有人可以帮助我解决这些问题吗?我只希望我的pin仅输入 4位数字,然后重新验证pin以继续进入菜单?

{
class program
{
    public static void Main()
    {

        int amount = 1000, deposit, withdraw;
        int choice, pin = 0, x = 0;
        Console.WriteLine("Enter Your Pin Number ");
        pin = int.Parse(Console.ReadLine());
        while (true)
        {
            Console.WriteLine("********Welcome to ATM Service**************\n");
            Console.WriteLine("1. Check Balance\n");
            Console.WriteLine("2. Withdraw Cash\n");
            Console.WriteLine("3. Deposit Cash\n");
            Console.WriteLine("4. Quit\n");
            Console.WriteLine("*********************************************\n\n");
            Console.WriteLine("Enter your choice: ");
            choice = int.Parse(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    Console.WriteLine("\n YOUR BALANCE IN Rs : {0} ", amount);
                    break;
                case 2:
                    Console.WriteLine("\n ENTER THE AMOUNT TO WITHDRAW: ");
                    withdraw = int.Parse(Console.ReadLine());
                    if (withdraw % 100 != 0)
                    {
                        Console.WriteLine("\n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100");
                    }
                    else if (withdraw > (amount - 500))
                    {
                        Console.WriteLine("\n INSUFFICENT BALANCE");
                    }
                    else
                    {
                        amount = amount - withdraw;
                        Console.WriteLine("\n\n PLEASE COLLECT CASH");
                        Console.WriteLine("\n YOUR CURRENT BALANCE IS {0}", amount);
                    }
                    break;
                case 3:
                    Console.WriteLine("\n ENTER THE AMOUNT TO DEPOSIT");
                    deposit = int.Parse(Console.ReadLine());
                    amount = amount + deposit;
                    Console.WriteLine("YOUR BALANCE IS {0}", amount);
                    break;
                case 4:
                    Console.WriteLine("\n THANK U USING ATM");
                    break;
            }
        }
        Console.WriteLine("\n\n THANKS FOR USING OUT ATM SERVICE");
    }
}
}

2 个答案:

答案 0 :(得分:1)

我建议这样:

chmod 777 /path/to/directory

如果您要验证预期长度为// read size (4) digits private static string ReadPin(int size = 4) { StringBuilder sb = new StringBuilder(size); while (sb.Length < size) { var key = Console.ReadKey(true); // we don't want to show the secret pin on the screen // Uncomment, if you want to let user escape entering the PIN // if (key.Key == ConsoleKey.Escape) { // return ""; // } if (key.KeyChar >= '0' && key.KeyChar <= '9') { sb.Append(key.KeyChar); Console.Write('*'); // let's show * instead of actual digit } } return sb.ToString(); } ... // private: there's no need for Main to be public private static void Main() { ... Console.WriteLine("Enter Your Pin Number "); int pin = int.Parse(ReadPin()); 的给定字符串(pin),则可以尝试 Linq

size

正则表达式

  using System.Linq;

  ...

  string pin = ...
  int size = 4;

  bool isValidPin = pin.Length == size && pin.All(c => c >= '0' && c <= '9');

答案 1 :(得分:0)

    int password; 
    int repassword

    Do{
    Console.WriteLine("\n Enter the password");
    password= int.Parse(Console.ReadLine()); //first password
    string ps = Convert.ToString(password);
    }while(ps.Length!=4) //request the password if is not composed by 4 digits

    //menu part//
    Do{
    Console.WriteLine("\n Reinsert the password");
    repassword= int.Parse(Console.ReadLine()); //reinsert password

    } while(repassword!=password)