【发布时间】:2016-03-16 18:17:12
【问题描述】:
我正在尝试制作一个凯撒密码程序,并且正在研究 Write 方法,但它只会打印出 System.Char[]。我该怎么做才能打印出重新格式化的字符串?我什至可以正确转换所有内容吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Caesar_Cipher
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Read or Write?");
string input1 = Console.ReadLine().ToLower();
if (input1 == "read")
{
}
if (input1 == "write")
{
write();
}
}
static void read()
{
}
public static void write()
{
Console.WriteLine("Write something to translate,");
string value = Console.ReadLine();
Console.WriteLine("How many shifts do you want?");
int shift = int.Parse(Console.ReadLine());
char[] Read = value.ToCharArray();
for (int i = 0; i < Read.Length; i++)
{
int b = i - (Read.Length + shift);
int c = Read.Length - shift;
if (b <= 0)
{
Read[c] = Read[i];
}
else
{
int shift1 = i + shift;
Read[shift1] = Read[i];
}
}
string d = Read.ToString();
Console.WriteLine(d);
}
}
}
【问题讨论】:
-
因为 Char 数组不知道如何将自身转换为字符串。所以基类 Object 启动并返回类型的名称。使用 string d = new string(Read);
标签: c# arrays encryption char