簡體   English   中英

使用C#從鍵盤獲取輸入?

[英]get input from keyboard using C#?

對於每種情況,我都試圖從用戶那里獲得兩次輸入,例如R,S,T。 現在,我只能一次獲得輸入。 如何分別獲得兩次輸入。

當我按S或T或R時,表格應自動關閉。

public class Program
{
    public static void Main()
    {
        string mystring = null; 
        Console.WriteLine("Enter your input : "); 
        mystring = Console.ReadLine();
        switch (mystring)
        {
            case "R": 
                Console.WriteLine("R");
                break; 

            case "S": 
                Console.WriteLine("S");
                break;

            case "T": 
                Console.WriteLine("T");
                break; 
        }        
    }      
}

OLD輸出:

Enter your input : 
R
R

所需的輸出:

Enter your input : 
R
R
Enter your input : 
S
S
Enter your input : 
T
T

除了連續兩次或三次獲得用戶輸入之外,我不會。

注意:當我按R,S或T鍵時,單詞形式應自動關閉。

您應該使用循環來完成這項工作,它會提示您輸入輸入,直到您輸入RST以外的內容:

bool loopCondition = true;
while (loopCondition)
{
    string mystring = null; Console.WriteLine("Enter your input : ");
    mystring = Console.ReadLine();
    switch (mystring)
    {
        case "R":
        case "S":
        case "T":
            Console.WriteLine(mystring);
            break;
        default:
            Console.WriteLine("Invalid Entry.. Exiting");
            loopCondition = false;
            break;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM