我正在嘗試加密已輸入的訊息(最多 36 個字符),方法是將其逐個寫入二維陣列。這些字母是逐行輸入的,但會逐列讀出以給出扭曲的版本。這是我到目前為止所得到的,但它只是列印出原始訊息和陣列中的訊息:
char[,] encrypter = new char[6, 6];
string message;
int count = 0;
Console.Write("Enter a message to encrypt: ");
message = Console.ReadLine();
Char[] messageToEncrypt = message.ToCharArray();
for (int r = 0; r < 6; r )
{
for (int c = 0; c < 6; c )
{
encrypter[r, c] = messageToEncrypt[count];
count = 1;
}
}
Console.Write(encrypter);
uj5u.com熱心網友回復:
我認為您正在尋找Chunk。
將序列的元素拆分為最大大小的塊。
在線試試吧!
using System;
using System.Linq;
public class Program
{
static void Main(string[] args)
{
var message = "This is a test";
var chunks = message.Chunk(6);
var lines = chunks.Select(x => string.Concat(x));
var output = string.Join(Environment.NewLine, lines);
Console.WriteLine(output);
}
}
輸出
This i
s a te
st
uj5u.com熱心網友回復:
代替存盤元件的行逐列的,交換的索引位置r和c
...
for (int r = 0; r < 6; r )
{
for (int c = 0; c < 6; c )
{
//Swap the index of r and c
encrypter[c,r] = messageToEncrypt[count];
count = 1;
}
}
在線試用
uj5u.com熱心網友回復:
二維陣列以行優先順序輸出(同一行上的行)。由于您也以相同的順序寫入陣列,因此列印時它不會顯得混亂。
顛倒你r和c回圈的順序:
for (int c = 0; c < 6; c )
{
for (int r = 0; r < 6; r )
{
encrypter[r, c] = messageToEncrypt[count];
count = 1;
}
}
網上試試
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/371004.html
標籤:C#
