这通过询问用户输入值拆分做了正确的方式,因为我是新的C#读三个输入
Console.WriteLine("please enter m,y,n: \n");
double month, year, numberOfMonths = Convert.ToDouble(Console.ReadLine());
这通过询问用户输入值拆分做了正确的方式,因为我是新的C#读三个输入
Console.WriteLine("please enter m,y,n: \n");
double month, year, numberOfMonths = Convert.ToDouble(Console.ReadLine());
你可以这样做通过一些分隔符如空格,分号等。然后分割值并进行相应的解析。例如
string input = Console.ReadLine();
string[] split = input.Split(',');
double month = Double.Parse(split[0]);
double year = Double.Parse(split[1]);
double numberofmonth = Double.Parse(split[2]);
Ofcourse上面的代码不是最优雅/高效/无错的代码。但是,它只是为了解决这个问题而写的。
在这条线上出现错误 string [] split = input.Split(“”); 拆分未突出显示 –
已更新。它现在应该工作。 – Ehsan
试试这个:
double month;
double year;
double numberOfMonths;
Console.WriteLine("please enter m \n");
month=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("please enter y \n");
year=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("please enter n \n");
numberOfMonths=Convert.ToDouble(Console.ReadLine());
您还可以存储输入数组,并告诉用户按每个输入后进入,然后做这个:
string[] input=new string[3];
Console.WriteLine("please enter m,y,n: \n");
for(int i=0;i<3;i++)
input[i]=Console.ReadLine();
,那么你可以将字符串转换输入任何你想要的数据类型, 上述不是一个非常有效的方法,但是你可以用它来从用户获得大量的输入,当所有输入具有相同的数据类型时,它很容易。
以这种方式,只有numberOfMonths将被分配。月和年不会被分配。 – Priyank