-3

私のアプリケーションは、A などの 1 文字を入力すると機能します。2 が 10 個表示されますが、入力した 10 桁すべてで機能するようにしたいです。何が間違っていますか? 1800HELLO2 と入力すると、すべての数字が表示されます。

class Program
{
    static void Main(string[] args)
    {
        int x = 0;
        string userInput;

        Console.WriteLine("Please enter the 10 digit telephone number. ");
        userInput = Console.ReadLine();

        while (x < 10) 
        {
            switch (userInput)
            {
                case "1":
                    userInput = "1";
                    x++;
                    break;
                case "A":
                case "B":
                case "C":
                case "2":
                    userInput = "2";
                    x++;
                    break;
                case "D":
                case "E":
                case "F":
                case "3":
                    userInput = "3";
                    x++;
                    break;
                case "G":
                case "H":
                case "I":
                case "4":
                    userInput = "4";
                    x++;
                    break;
                case "J":
                case "K":
                case "L":
                case "5":
                    userInput = "5";
                    x++;
                    break;
                case "M":
                case "N":
                case "O":
                case "6":
                    userInput = "6";
                    x++;
                    break;
                case "P":
                case "Q":
                case "R":
                case "7":
                    userInput = "7";
                    x++;
                    break;
                case "S":
                case "T":
                case "U":
                case "8":
                    userInput = "8";
                    x++;
                    break;
                case "V":
                case "W":
                case "X":
                case "Y":
                case "Z":
                    userInput = "9";
                    x++;
                    break;
                case "0":
                    userInput = "0";

                    break;
            }


            Console.WriteLine(userInput);
        }
        }
    }
}
4

4 に答える 4

3

ユーザー入力は任意の数の文字にすることができます (ReadLine()関数は、 を押すまでNENTER文字を読み取ります) が、switchステートメントは入力の個々の文字をチェックしません。1 文字の文字列の数だけをチェックします。

これは、 と入力しても1-800-333-1111switch.

入力文字列の各文字を 1 つずつ反復処理し、個々の文字を確認する必要があります。例えば:

if ( userinput != null )
{
    userinput = userinput.ToUpper ();

    for ( int i = 0; i < userinput.Length; i++ )
    {
        switch ( userinput[i] )
        {
            case '1':
            case 'A':
                ...
                break;

            ...

            default:
                // Handle invalid characters here
                break;
        }
    }
}

さまざまな大文字と小文字の値は'、1 文字の文字列ではなく、1 文字 ( を使用) であることに注意してください。

電話番号の長さをコード内の数値としてハードコードすることはあまり良い考えではないことに注意してください。ユーザーによって電話番号の入力方法が異なる場合があります。セパレータとしてスペースやダッシュを使用するユーザーもいれば、数字のみを入力するユーザーもいます。これらの場合、入力文字列の長さは異なります。一部のユーザーは、誤って複数のスペースを入力したり、市外局番を入力したりすることさえあり(ます)

チェックする前に入力を検証するか、入力を反復するときに入力桁数に依存しないでください。

于 2013-08-24T01:30:27.170 に答える
2

あなたのアプローチは本当に正しくありません。文字列内の各文字を実際にチェックしたいときは、毎回 userInput をオンにしています。以下のコードを調べてください。

using System;

class Program {
    static void Main(string[] args) {


        Console.WriteLine("Please enter the 10 digit telephone number. ");
        string userInput = Console.ReadLine();
        // Maybe do some validation here, check the length etc
        Char output;
        foreach (Char c in userInput) {
            switch (c) {
                case 'A':
                case 'B':
                case 'C':
                    output = '2';
                    break;
                case 'D':
                case 'E':
                case 'F':
                    output = '3';
                    break;
                case 'G':
                case 'H':
                case 'I':
                    output = '4';
                    break;
                case 'J':
                case 'K':
                case 'L':
                    output = '5';
                    break;
                case 'M':
                case 'N':
                case 'O':
                    output = '6';
                    break;
                case 'P':
                case 'Q':
                case 'R':
                    output = '7';
                    break;
                case 'S':
                case 'T':
                case 'U':
                    output = '8';
                    break;
                case 'V':
                case 'W':
                case 'X':
                case 'Y':
                case 'Z':
                    output = '9';
                    break;
                default:
                    output = c;
                    break;
            }

            Console.Write(output);
        }
        Console.WriteLine();
        Console.ReadLine();
    }
}

ループを使用しているforeachため、長さをハードコーディングしていないため、柔軟性が得られます。

于 2013-08-24T01:35:49.777 に答える
1

代わりにこれを試してください:

class Program
{ 
    static void Main(string[] args)
    {
        int x = 0;
        string userInput;

        Console.WriteLine("Please enter the 10 digit telephone number. ");
        userInput = Console.ReadLine();

        // Did the user type in more than 10 characters?
        if(userInput.Length > 10)
        {
            // Get the first ten letters, no matter how many letters the user entered
            userInput = userInput.Substring(0, 10);
        }

        // Force values to upper case for comparison
        userInput = userInput.ToUpper();

        string systemOutput = String.Empty;
        foreach(var c in userInput) 
        {
            switch (c)
            {
                case "1":
                    systemOutput += "1";
                    break;
                case "A":
                case "B":
                case "C":
                case "2":
                    systemOutput += "2";
                    break;
                case "D":
                case "E":
                case "F":
                case "3":
                    systemOutput += "3";
                    break;
                case "G":
                case "H":
                case "I":
                case "4":
                    systemOutput += "4";
                    break;
                case "J":
                case "K":
                case "L":
                case "5":
                    systemOutput += "5";
                    break;
                case "M":
                case "N":
                case "O":
                case "6":
                    systemOutput += "6";
                    break;
                case "P":
                case "Q":
                case "R":
                case "7":
                    systemOutput += "7";
                    break;
                case "S":
                case "T":
                case "U":
                case "8":
                    systemOutput += "8";
                    break;
                case "V":
                case "W":
                case "X":
                case "Y":
                case "Z":
                    systemOutput += "9";
                    break;
                case "0":
                    systemOutput += "0";
                    break;
            }
        }

        Console.WriteLine(systemOutput);
    }
}
于 2013-08-24T01:43:43.047 に答える
0

Dictionaryこの目的のために以下を利用できます。

static void Main(string[] args)
{
    int x = 0;
    string userInput;

    Console.WriteLine("Please enter the 10 digit telephone number. ");
    userInput = Console.ReadLine();
    Dictionary<string,string> dict = new Dictionary<string,string>();
    dict.Add("1","1"); 
    dict.Add("ABC2","2");
    dict.Add("DEF3","3");
    dict.Add("GHI4","4");
    dict.Add("JKL5","5");
    dict.Add("MNO6","6");
    dict.Add("PQR7","7");
    dict.Add("STU8","8");
    dict.Add("VWXYZ9","9");
    dict.Add("0","0");
    userInput = string.Join("",userInput.Select(c=>dict.First(k=>k.Key.Contains(c)).Value).ToArray());
    Console.WriteLine(userInput);
}

またはさらに簡潔に:

static void Main(string[] args)
{
    int x = 0;
    string userInput;

    Console.WriteLine("Please enter the 10 digit telephone number. ");
    userInput = Console.ReadLine();
    string[] s = "0,1,ABC2,DEF3,GHI4,JKL5,MNOP6,PQR7,STU8,VWXYZ9".Split(',');
    userInput = string.Join("",userInput.Select(c=>s.Select((x,i)=>new{x,i})
                                                    .First(k=>k.x.Contains(c)).i).ToArray());
    Console.WriteLine(userInput);
}
于 2013-08-24T04:47:07.090 に答える