【发布时间】:2019-01-09 15:59:33
【问题描述】:
所以,我的任务是我必须编写一个程序来更改字符串并将其按正确的顺序放置。现在,它只被允许做三件事,其中一件我有疑问。允许取第二个字符并将其移动到第三个字符,依此类推,直到您到达倒数第二个字符。这个被倒数第三个字符取代。因此,abcdef 将变为 aebcdf。我的代码给了我输出 aebbbf。我有这个:
class Program{
static void Main(string[] args)
{
var p = new Program();
string input = Console.ReadLine();
char[] characters = new char[input.Length];
characters = input.ToCharArray();
string answer = Console.ReadLine();
if (answer == "x")
{
p.MethodX(characters);
string s = new string(characters);
Console.WriteLine(s);
}
Console.ReadKey();
}
}
还有方法MethodX:
public void MethodeX(char[] input)
{
int lengthText = input.Length;
char temp = input[lengthText - 2];
for (int i = 1; i < input.Length - 2; i++)
{
input[i + 1] = input[i];
input[1] = temp;
}
}
提前感谢您的帮助!
【问题讨论】:
-
听起来很像Bubblesort
-
你的问题好像是homework。