如何制作一个程序,将char数组的第一行更改为另一行,并更改C#中字符的顺序?

How to make a program that changes the first row of a char array with another row, and changes the order of the characters in C#?

提问人:goat12345 提问时间:11/7/2023 最后编辑:Petter Hesselberggoat12345 更新时间:11/7/2023 访问量:24

问:

我需要一个程序来更改字符数组的行并更改字符的顺序,例如:a,b,c>c,b,a。

我不知道如何制作它,我只是有一个想法,我需要它,因为我开始学习 C#。所以是的,我想成为一名程序员,他们说我需要从 C# 开始。

C# 字符数组

评论


答:

0赞 Goat 11/7/2023 #1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{

    static void reversed(char[,] array) 
    {
        int i = 0;
        int j = 0;
       
        for (j = 0; j < 3; j++)
        {
            char temp = array[0, j];
            array[0, j] = array[1, j];
            array[1, j] = temp;
        }

        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
            {
                Console.Write(array[i, j] + " ");
            }

            Console.WriteLine("\n");
        }
    }

    static void reverse(char[,] array) 
    {
        Console.WriteLine("\n");
        Console.WriteLine("\n");
        for (int i = 0; i < 3; i++)
        {         
            char[] rword = new char[]{array[i, 0], array[i,1], 
 array[i,2]};
            Array.Reverse(rword);
            for (int j = 0; j < 3; j++)
            {
                array[i, j] = rword[j];
                Console.Write(array[i,j]+ " ");
            }
            Console.WriteLine("\n");
        }
    }

    static void Main(string[] args)
    {
        char[,] charArr = new char[3, 3] 
        {
        {'A', 'B', 'C'},
        {'D', 'E', 'F'},
        {'G', 'H', 'I'},
        };

        reversed(charArr);
        reverse(charArr);
        Console.ReadKey();
    }
}
}

评论

1赞 Alexei Levenkov 11/7/2023
仅代码答案很少有用。请提供代码的解释。请注意,问题本身缺少最小的可重现示例,并且没有表现出任何努力......您可能希望先编辑问题,而不是提供答案(尤其是使用硬编码数组大小且没有解释的答案)。