0

単純なログイン/認証コンソールアプリケーションを作成しようとしています。たとえば、パスワードとして文字列testpwdがあり、ユーザーがパスワードの入力を開始した瞬間の時間をミリ秒単位でカウントし、その数を出力する必要があります。ユーザーが機能を使用してキーボードから入力を開始するたびに、各ユーザーがパスワードを入力するのにかかる秒GetTickCount数。

どうすればよいかわかりませんが、私が何とかできたのは、以下のコードだけです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace LoginSystem
{
    class LSystem
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello! This is simple login system!");
            Console.Write("Write your username here: ");
            string strUsername = Console.ReadLine();
            string strTUsername = "testuser";
            if (strUsername == strTUsername)
            {
                Console.Write("Write your password here: ");
                Console.ForegroundColor = ConsoleColor.Black;
                string strPassword = Console.ReadLine();
                string strTPassword = "testpwd";
                if (strPassword == strTPassword)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("You are logged in!");
                    Console.ReadLine();

                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("Bad password for user: {0}", strUsername);
                    Console.ReadLine();
                }
            }
            else
            {
                Console.WriteLine("Bad username!");
                Console.ReadLine();
            }
        }
    }
}
4

3 に答える 3

0

1- DateTime.Now を使用して、それらを減算して期間を取得できます。

2- GetTickCount を呼び出しますが、最初に次のように宣言する必要があります。

[DllImport("kernel32.dll")]
static extern uint GetTickCount();
于 2013-03-16T15:42:07.853 に答える
0

シンプルなストップウォッチ? コードの関連部分はこのように書くことができます

...
Console.ForegroundColor = ConsoleColor.Black;
StopWatch sw = new Stopwatch();
sw.Start();
string strPassword = Console.ReadLine();
sw.Stop()
TimeSpan ts = sw.Elapsed;
string strTPassword = "testpwd";
if (strPassword == strTPassword)
{
    Console.ForegroundColor = ConsoleColor.Gray;
    Console.WriteLine("You are logged in after " + ts.Milliseconds.ToString() + " milliseconds");
    Console.ReadLine();
}
.....
于 2013-03-16T15:42:25.073 に答える
0

まず、あなたの質問は理解するのが難しいです。私が正しく読んだ場合、ユーザーが入力を開始したときにタイミングを開始し、ユーザーがEnterキーを押したときに停止しますか? System.Diagnostics.Stopwatchクラスの使用はどうですか?

Console.ReadLine() を呼び出す直前に、新しい Stopwatch() を開始してから、Start() メソッドを呼び出します。

Console.ReadLine() の直後に、ストップウォッチを停止します。

        Console.Write("Write your username here: ");

        var stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();

        string strUsername = Console.ReadLine();

        stopwatch.Stop();

        string strTUsername = "testuser";
于 2013-03-16T15:46:15.083 に答える