-3

如果我输入 1 个字符,例如 A,我的应用程序就可以工作。它会给我 10 个 2,但我希望它可以在我输入的所有 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()函数将读取N个字符,直到您按下ENTER),但您的switch语句不会检查输入的单个字符 - 它只检查多个 1 字符的字符串。

这意味着即使您键入1-800-333-1111,您也永远不会检查它,因为它不是一个 1 字符的字符串,就像您在switch.

您需要逐个遍历输入字符串中的每个字符并检查各个字符。例如:

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 个字符的字符串。

请注意,将电话号码的长度硬编码为代码中的数字并不是一个好主意。不同的用户输入电话号码的方式可能不同——有些用户可能使用空格或破折号作为分隔符,有些用户可能只输入数字。在这些情况下,输入字符串的长度会有所不同。一些用户甚至可能不小心输入了多个空格或输入(和输入)区号。

您应该在检查之前验证输入,或者在迭代输入时不应该依赖输入位数。

于 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 回答