C#字符增量
我是C#的新手,所以这个问题(希望)会非常简单 我试图增加字符数组中的每个字符。这是我的密码C#字符增量,c#,increment,C#,Increment,我是C#的新手,所以这个问题(希望)会非常简单 我试图增加字符数组中的每个字符。这是我的密码 //increment each character in array for (int i = 0; i < text.Length; i++) { textArray[i]++; //this works // textArray[i] +=13; //this doesn't work
//increment each character in array
for (int i = 0; i < text.Length; i++)
{
textArray[i]++; //this works
// textArray[i] +=13; //this doesn't work
}
//增加数组中的每个字符
for(int i=0;i
我可以将数组增加1,但不能超过1
谢谢
如果有帮助的话,下面是我的其余代码
// put all text into a string - here loosely typed as a var
var s = System.IO.File.ReadAllText(@"C:\Users\Eliezer Feder\Documents\2 Landers\Limudie Chol\5th Semester\C#\GettysburgAddress.txt");
var upper = s.ToUpper();
string text = ""; //empty string to add characters to it if they are indeed characters.
foreach (char c in upper.ToCharArray())
{
if (Char.IsLetter(c))
{
text += c;
}
}
//change the 'text' string to an array so can increment each individual char
Char[] textArray = text.ToArray();
//output old text in the char array:
Console.WriteLine(textArray);
Console.ReadKey();
//increment each character in array
for (int i = 0; i < text.Length; i++)
{
textArray[i]++; //this works
// textArray[i] +=13; //this doesn't work
}
Console.WriteLine(textArray);
Console.ReadKey();
//change back to string so can write to file:
string lines = ""; //empty string to add characters to it if they are characters.
foreach (char c in upper.ToCharArray())
{
lines += textArray[c];
}
System.IO.File.WriteAllLines(@"Eliezer Feder\Documents\2 Landers\Limudie Chol\5th Semester\C#\encrypted.txt", lines); //THIS PART IS ALSO NOT WORKING FOR SOME REASON
//将所有文本放在一个字符串中-此处以变量的形式松散键入
var s=System.IO.File.ReadAllText(@“C:\Users\Eliezer Feder\Documents\2 Landers\Limudie Chol\5th Semment\C#\GettysburgAddress.txt”);
var upper=s.ToUpper();
字符串文本=”//空字符串,如果它们确实是字符,则向其中添加字符。
foreach(上部.ToCharArray()中的字符c)
{
if(字符(c))
{
text+=c;
}
}
//将“文本”字符串更改为数组,以便可以增加每个字符
Char[]textArray=text.ToArray();
//在字符数组中输出旧文本:
Console.WriteLine(textArray);
Console.ReadKey();
//增加数组中的每个字符
for(int i=0;i
文本数组的元素类型是char
。文字13
的类型为int
。将int
添加到char
的结果是int
,因此不能将其分配给char
变量
您必须将文本强制转换为char
,然后结果也将是char
:
textArray[i] += (char)13;
你说它不起作用是什么意思?你有例外吗?您的期望是什么?请参阅始终包含完整的错误消息(来自异常或编译器)并首先搜索该消息。副本在标题中使用它。