寻求一些帮助。我确定它很容易,但我似乎无法摆脱它的困扰。将C#字符串数组加倍
首先,这是我到目前为止的代码:
//Prompts user to enter numbers
Console.Write("Enter a line of comma-seperated temperatures: ");
string temps = Console.ReadLine();
//Splits commas, seperating numbers into array
string[] numsInString = temps.Split(',');
int temps1 = numsInString.Length;
int[] temps2 = new int[temps1];
for (int i = 0; i < numsInString.Length; i++)
{
temps2[i] = int.Parse(numsInString[i]);
}
Console.WriteLine("Minimum temp: " + temps2.Min());
Console.WriteLine("Average temp: " + temps2.Average());
所以,它会提示用户输入的温度即“5”用逗号隔开,“5,6,7,8”。我的麻烦是我不能有小数点范围的温度,例如“5.4,5.7,6.3,6.8”。我发现我需要将字符串转换为double,但我不完全确定如何做到这一点。
感谢
您首先想到的可能是,你可以尝试更换所有出现' int'与'double',所以你有一个'double []'并且调用'double.Parse'。如果你这样做,它可能会奏效。 –
@CharlesMager我曾尝试过,但出现错误“错误1 \t不能隐式地将类型'双'转换为'int'。存在明确的转换(你是否缺少一个转换?)”我想我可能不得不删除或重新安排一些代码,不太确定 - 而这些错误指向[] – royalrekesh
之间的所有内容。再仔细一点,你不希望将'int temps1'和'int i'替换为'Length'仍然是'int'。你只想改变'temps2'数组的类型并将'int.Parse'改为'double.Parse'。 –