当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Ask user to enter parameters in console application

问题描述:

I have a simple method with two double type parameter. Like below:

Rectangle r = new Rectangle();

r.getArea(Convert.ToDouble(Console.ReadLine()), Convert.ToDouble(Console.ReadLine()));

That getArea method has two double type parameters. So, how do I ask the user to input those double type values. As this code user gives those value, but without any message. I want them to insert one by one after they see a message such as;

console.writeLine(@"Please Enter A Double Value : ");

网友答案:

You can ask user to input comma or space separated values and then parse the input string using string.Split method.

Console.writeLine(@"Please enter a space separeted double Values : ");    
var paramList= Console.ReadLine().Split(' '); // ',' for comma separeted values
var param1 = Convert.ToDouble(paramList[0]);
var param2 = Convert.ToDouble(paramList[1]);

Also, if user input contains more then two parameters, you can parse the string to an array of double parameters.

List<double> paramList = Console.ReadLine()
     .Split(' ')
     .Select(x => Convert.ToDouble(x))
     .ToList();
网友答案:
console.writeLine(@"Please Enter A Double Value : ");
string double1 = Console.ReadLine();
console.writeLine(@"Please Enter A Double Value : ");
string double2 = Console.ReadLine();
分享给朋友:
您可能感兴趣的文章:
随机阅读: