【发布时间】:2021-12-15 02:24:44
【问题描述】:
//read
Console.Write("Please enter (pyramid slot number,block letter,whether or not the block should be lit): ");
string csvString = Console.ReadLine();
//location of comma
int firstComma = csvString.IndexOf(',');
int secondComma = csvString.IndexOf(',', firstComma + 1);
//extract slot number of pyramid
int slotNumber = int.Parse(csvString.Substring(0, firstComma));
string blockLetter = csvString.Substring(firstComma + 1, secondComma);
Boolean lit = Boolean.Parse(csvString.Substring(secondComma + 1));
//print
Console.WriteLine("Pyramid Slot Number: " + slotNumber);
Console.WriteLine("Block Letter: " + blockLetter);
Console.WriteLine("Lit: " + lit);
我尝试输入“5,M,true”。然而,Block Letter 的输出是“M,t”。如果我尝试输入 15 而不是 5,那么它会给出“M,tr”。最后,我只想收到一封信。解决这个问题后我会使用char。
编辑:
char blockLetter = char.Parse(csvString.Substring(firstComma + 1, 1));
我用这个谢谢!
【问题讨论】:
-
我认为你的代码中的错误是使用了 Substring 方法,应该传递 (index, length),而你传递 (index, other_index)。您需要通过 (index, other_index - index) 代替。见docs.microsoft.com/pl-pl/dotnet/api/…