C# 程序跳过代码行

C# 程序跳过代码行,c#,C#,我有一个改变字符数组值的小程序。但首先,您需要告诉中的程序要更改哪个数组的值;并将新值及其位置写入同一数组位置 但当我输入数组号时,程序跳过代码行,允许输入新值及其位置。然后程序在最后抛出一个FormatException 代码如下: static void addLetters(char[] messageOne, char[] messageTwo) { char Mnumber; char letter; string pos

我有一个改变字符数组值的小程序。但首先,您需要告诉中的程序要更改哪个数组的值;并将新值及其位置写入同一数组位置

但当我输入数组号时,程序跳过代码行,允许输入新值及其位置。然后程序在最后抛出一个FormatException

代码如下:

    static void addLetters(char[] messageOne, char[] messageTwo)
    {
        char Mnumber;
        char letter;
        string pos;
        int position;

        Console.Write("- Message #: ");
        Mnumber = (char)Console.Read();

        if (Mnumber == '1')
        {
            Console.Write("Letter: ");
            letter = (char)Console.Read();

            Console.Write("\nPosition: ");
            pos = Console.ReadLine();
            position = Int32.Parse(pos);

            messageOne[position - 1] = letter;
        }

        if (Mnumber == '2')
        {
            Console.Write("Letter: ");
            letter = (char)Console.Read();

            Console.Write("\nPosition: ");
            pos = Console.ReadLine();
            position = Int32.Parse(pos);

            messageTwo[position - 1] = letter;
        }
    }


    static void Main(string[] args)
    {
        char[] array1 = new char[50];
        char[] array2 = new char[50];

        for (int i = 0; i < 50; i++)
        {
            array1[i] = '*';
            array2[i] = '*';
        }

        addLetters(array1, array2);
    }
}
static void addLetters(char[]messageOne,char[]messageTwo)
{
字符数;
字符字母;
字符串位置;
内部位置;
控制台。写(“-Message#::”);
Mnumber=(char)Console.Read();
如果(Mnumber=='1')
{
安慰。写(“信:”);
字母=(char)Console.Read();
控制台。写入(“\n位置:”);
pos=Console.ReadLine();
position=Int32.Parse(pos);
messageOne[位置-1]=字母;
}
如果(Mnumber=='2')
{
安慰。写(“信:”);
字母=(char)Console.Read();
控制台。写入(“\n位置:”);
pos=Console.ReadLine();
position=Int32.Parse(pos);
messageTwo[位置-1]=字母;
}
}
静态void Main(字符串[]参数)
{
字符[]数组1=新字符[50];
char[]array2=新字符[50];
对于(int i=0;i<50;i++)
{
阵列1[i]=“*”;
阵列2[i]='*';
}
添加字母(第1列、第2列);
}
}

另外,您能告诉我如何使这段代码更“优雅”吗?

在读取单个字符时,建议使用Console.ReadKey().KeyChar。我已经修改了您的代码以处理异常:

    static void addLetters(char[] messageOne, char[] messageTwo)
    {
        char Mnumber;
        char letter;
        string pos;
        int position;

        try
        {
            do
            {
                Console.Write("- Message #: ");
                Mnumber = Console.ReadKey().KeyChar;
                Console.WriteLine();
            }
            while (Mnumber != '1' && Mnumber != '2');

            Console.Write("\nLetter: ");
            letter = Console.ReadKey().KeyChar;

            Console.Write("\nPosition: ");
            pos = Console.ReadLine();
            position = Int32.Parse(pos);

            if (Mnumber == '1')
                messageOne[position - 1] = letter;
            else
                messageTwo[position - 1] = letter;
        }
        catch (FormatException)
        {
            Console.WriteLine("Invalid input format, position must be an integer");
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("Position out of array bounds");
        }
        finally
        {
            Console.Read();
        }

    }

对这两条消息尝试一个函数,并将字符串转换为整数

    static char ReadChar(string prompt)
    {
        // Screen Prompt
        Console.Write(prompt);
        // Read a Character
        return Console.ReadKey(false).KeyChar;
    }
    static int ReadNumber(string prompt)
    {
        // Screen Prompt
        Console.Write(prompt);
        int result=-1;
        // Reads a number and onverts it into an integer
        int.TryParse(Console.ReadLine(), out result);
        return result;
    }

    // Caution, this modifies the contents of `messages`
    static void addLetters(params char[][] messages)
    {
        // Read message index (first=1, second=2, etc)
        int Mnumber=ReadNumber("- Message #: ");
        // Read letter
        char letter=ReadChar("Letter: ");
        // Read placement position
        int position=ReadNumber("\nPosition: ");
        // Get the right message
        char[] current_message=messages[Mnumber-1];
        // Assigns a letter to the message
        current_message[position]=letter;
    }
    static void Main(string[] args)
    {
        int n=50; //Store the size instead of hard coding it all over your code
        char[] array1=new char[n];
        char[] array2=new char[n];

        for (int i=0; i<n; i++)
        {
            array1[i]='*';
            array2[i]='*';
        }

        addLetters(array1, array2);
    }
static char ReadChar(字符串提示)
{
//屏幕提示
控制台。写入(提示);
//读一个字
返回控制台.ReadKey(false).KeyChar;
}
静态int ReadNumber(字符串提示)
{
//屏幕提示
控制台。写入(提示);
int结果=-1;
//读取数字并将其转换为整数
int.TryParse(Console.ReadLine(),out结果);
返回结果;
}
//注意,这会修改“消息”的内容`
静态void addLetters(params char[][]消息)
{
//读取消息索引(第一个=1,第二个=2,以此类推)
int Mnumber=ReadNumber(“-Message#::”);
//读信
char letter=ReadChar(“字母:”);
//读取放置位置
int位置=读取编号(“\n位置:”);
//得到正确的信息
char[]当前消息=消息[Mnumber-1];
//为邮件指定一个字母
当前_消息[位置]=字母;
}
静态void Main(字符串[]参数)
{
int n=50;//存储大小,而不是将其硬编码到整个代码中
char[]array1=新字符[n];
char[]array2=新字符[n];

对于(inti=0;我告诉我们哪些行给出了错误。
position=Int32.Parse(pos)
行给出一个错误您可能想使用
TryParse
而不是
Parse
。如果它在那一行,我们需要知道您输入的内容。我什么都不输入,因为程序在我输入消息编号后会跳过这些行。我的意思是它打印字母:和位置:但不允许我输入任何内容,只需转到功能结束