大小写、中断和使用c#循环

本文关键字:循环 中断 大小写 | 更新日期: 2023-09-27 18:28:47

如果我键入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);
        }
        }
    }
}

大小写、中断和使用c#循环

您的用户输入可以是任意数量的字符(ReadLine()函数将读取N个字符,直到您按下ENTER),但switch语句不检查输入的单个字符,它只检查多个1个字符的字符串。

这意味着,即使您键入1-800-333-1111,也永远不会检查它,因为它不像switch中的各种情况那样是一个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个字符的字符串。

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

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

您的方法并不正确。当你真的想检查字符串中的每个字符时,你每次都会打开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循环,所以我们不需要对长度进行硬编码,这提供了灵活性。

试试这个:

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);
    }
}

您可以为此目的利用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);
}