C# 对于循环问题,';i';不会返回到零
我不知道还有什么方法可以让循环运行,检查字母是否大写,通过添加“C”(或否),然后继续字符串中的其余字符。我不太擅长解释这里发生了什么,因为我对C#有点陌生。但是“for”循环中的“i”永远不会重置回0,从而使您收到错误的输出C# 对于循环问题,';i';不会返回到零,c#,C#,我不知道还有什么方法可以让循环运行,检查字母是否大写,通过添加“C”(或否),然后继续字符串中的其余字符。我不太擅长解释这里发生了什么,因为我对C#有点陌生。但是“for”循环中的“i”永远不会重置回0,从而使您收到错误的输出 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Encrypt { class Program {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Encrypt
{
class Program
{
static char[] alphabet = new char[52] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'z' };
static void Main(string[] args)
{
string input = Console.ReadLine();
string output = Decode(input);
Console.WriteLine(output);
Console.ReadLine();
}
static string Decode(string input)
{
string output = "";
foreach (Char c in input)
{
for (int i = 0; i < alphabet.GetLength(0); i++)
{
if (c == alphabet[i])
{
int intoutput = i + 1;
if (intoutput > 26)
{
intoutput = intoutput - 26;
output = "C" + intoutput + " ";
}
else
{
output = intoutput + " ";
}
}
}
}
return output;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间加密
{
班级计划
{
静态字符[]字母表=新字符[52]{a',b',c',d',e',f',g',h',i',j',k',l',m',n',o',p',q',r',s',t',u',w',x',y',z',a',b',c',d',e',f',g',h',i',j',k',l',m',n',o',p',q',r',t',u',v',w',x',y',y',z'};
静态void Main(字符串[]参数)
{
字符串输入=Console.ReadLine();
字符串输出=解码(输入);
控制台写入线(输出);
Console.ReadLine();
}
静态字符串解码(字符串输入)
{
字符串输出=”;
foreach(输入中的字符c)
{
for(int i=0;i26)
{
输入输出=输入输出-26;
输出=“C”+输入输出+”;
}
其他的
{
输出=输入输出+“”;
}
}
}
}
返回输出;
}
}
}
您的问题不在于重置i
,而在于
output = "C" + intoutput + " ";
...
output = intoutput + " ";
它们不会附加到输出
,而是分配给输出
,这意味着以前分配的值会丢失。你会想要像这样的东西
output = output + "C" + intoutput + " ";
...
output = output + intoutput + " ";
您正在覆盖每个迭代中的输出 输出+=“C”+输入输出+”;
输出+=输入输出+“” 你说的
output=
,它将输出重新分配到一个不同的值,它不会增加它,而是你想要输出+=
此外,字母字符按顺序存储在字符集中('b'='a'+1
),这应该可以:
foreach (Char c in input)
{
if ('A' <= c <= 'Z')
output += "C" + (int)(c - 'A') + " ";
else if ('a' <= c <= 'z')
output += (int)(c - 'a') + " ";
}
以上两种算法都比您的算法快得多,因为它不必循环遍历1-52个值,只需进行1-4个比较。您的答案到此为止
var output = new StringBuilder();
foreach (Char c in input)
{
for (int i = 0; i < alphabet.Length; i++)
{
if (c == alphabet[i])
{
if (Char.IsUpper(c))
output.AppendFormat("C{0} ", i-26);
else
output.AppendFormat("{0} ",i);
}
}
}
return output.ToString();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Encrypt
{
class Program
{
static char[] alphabet = new char[52] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'z' };
static void Main(string[] args)
{
string input = Console.ReadLine();
string output = Decode(input);
Console.WriteLine(output);
Console.ReadLine();
}
static string Decode(string input)
{
string output = "";
foreach (Char c in input)
{
for (int i = 0; i < alphabet.GetLength(0); i++)
{
if (c == alphabet[i])
{
int intoutput = i + 1;
if (intoutput > 26)
{
intoutput = intoutput - 26;
output = output + "C" + intoutput + " ";
}
else
{
output = output + intoutput + " ";
}
}
}
}
return output;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间加密
{
班级计划
{
静态字符[]字母表=新字符[52]{a',b',c',d',e',f',g',h',i',j',k',l',m',n',o',p',q',r',s',t',u',w',x',y',z',a',b',c',d',e',f',g',h',i',j',k',l',m',n',o',p',q',r',t',u',v',w',x',y',y',z'};
静态void Main(字符串[]参数)
{
字符串输入=Console.ReadLine();
字符串输出=解码(输入);
控制台写入线(输出);
Console.ReadLine();
}
静态字符串解码(字符串输入)
{
字符串输出=”;
foreach(输入中的字符c)
{
for(int i=0;i26)
{
输入输出=输入输出-26;
输出=输出+“C”+输入输出+“”;
}
其他的
{
输出=输出+输入输出+“”;
}
}
}
}
返回输出;
}
}
}
你能解释一下你希望输出是什么吗?例如输入abcd和abcd?@JamesKyburz我想“abcd”
->“0 1 2 3”
,“abcd”
->“C0 C1 C2 C3”
。我知道,循环很好,但每次迭代时你都会覆盖输出中的值。
return string.Join(" ", input.ToCharArray().Select(c =>
{
for (int i = 0; i < alphabet.Length; i++)
{
if (c == alphabet[i])
return Char.IsUpper(c) ?
string.Format("C{0}", i - 26)
:
i.ToString();
}
return "";
}).ToArray()).Trim();
//static List<char> alphabet = new List<char> { 'a', 'b', 'c'...};
int i;
return string.Join(" ", input.ToCharArray().Select(c =>
(i = alphabet.IndexOf(c)) != -1 ?
(Char.IsUpper(c) ?
string.Format("C{0}", i - 26)
:
i.ToString()
)
:
""
).ToArray());
}
return string.Join(" ", input.ToCharArray().Select(c =>
Char.IsLetter(c) ?
(Char.IsUpper(c) ?
string.Format("C{0}", c-'A')
:
(c-'a').ToString()
)
:
""
).ToArray()).Trim();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Encrypt
{
class Program
{
static char[] alphabet = new char[52] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'z' };
static void Main(string[] args)
{
string input = Console.ReadLine();
string output = Decode(input);
Console.WriteLine(output);
Console.ReadLine();
}
static string Decode(string input)
{
string output = "";
foreach (Char c in input)
{
for (int i = 0; i < alphabet.GetLength(0); i++)
{
if (c == alphabet[i])
{
int intoutput = i + 1;
if (intoutput > 26)
{
intoutput = intoutput - 26;
output = output + "C" + intoutput + " ";
}
else
{
output = output + intoutput + " ";
}
}
}
}
return output;
}
}
}