[英]How to calculate the surface of a square with user entries in C#?
I would like to write a program where the user will be asked for numbers which will be used to calculate the surface of a square.我想编写一个程序,用户将被要求输入用于计算正方形表面的数字。 But I don't know how to "use" the entered numbers in my methods.但我不知道如何在我的方法中“使用”输入的数字。
Thank you谢谢
Here is what I have done so far这是我到目前为止所做的
static void Main(string[] args)
{
calculate();
}
public static void Height()
{
Console.WriteLine("Enter height of the square");
string enter1 = Console.ReadLine();
double height = Convert.ToDouble(enter1);
// height of the square
}
public static void Length()
{
Console.WriteLine("Enter length of the square");
string enter2 = Console.ReadLine();
double length = Convert.ToDouble(enter2);
//length of the square
}
static double calculate(double a, double b)
{
double summary = a * b;
Console.WriteLine(summary);
return summary;
}
Let's return and use the input you have first, to make sure you have some success:让我们返回并使用您首先拥有的输入,以确保您取得了一些成功:
static void Main(string[] args)
{
calculate(Height(), Length());
}
public double void Height()
{
Console.WriteLine("Enter height of the square");
string enter1 = Console.ReadLine();
return Convert.ToDouble(enter1);
// height of the square
}
public static double Length()
{
Console.WriteLine("Enter length of the square");
string enter2 = Console.ReadLine();
return Convert.ToDouble(enter2);
//length of the square
}
static double calculate(double a, double b)
{
double summary = a * b;
Console.WriteLine(summary);
return summary;
}
}
If there are no typos on my part, then this should work for you, but you still have a LOT to learn.如果我没有错别字,那么这应该对你有用,但你还有很多东西要学。 You should separate your I/O operations from the business logic that you compute and should only have a thin communication between the two for the sake of reusability and portability.您应该将您的 I/O 操作与您计算的业务逻辑分开,并且为了可重用性和可移植性,两者之间应该只进行少量通信。 I didn't do it in the code above, it's left for you as a homework.我在上面的代码中没有这样做,它留给你作为家庭作业。
First I suggest extracting a method (reading double
value):首先我建议提取一个方法(读取double
值):
public static double ReadDouble(string title) {
// while (true) - keep on asking until valid value provided
while (true) {
if (!string.IsNullOrEmpty(title))
Console.WriteLine(title);
// TryParse: user can enter any input here, say, "bla-bla-bla"
// we don't want any exceptions here
if (double.TryParse(Console.ReadLine(), out double result))
return result;
Console.WriteLine("Sorry, not a valid floating point value; please, try again");
}
}
Then we can implement the main routine:然后我们就可以实现主程序了:
static void Main(string[] args)
{
double height = ReadDouble("Enter height of the rectangle");
double width = ReadDouble("Enter width of the rectangle");
double square = height * width;
Console.WriteLine($"The square of the {width} x {height} rectangle is {square}");
// Pause (wait for a keypress) for user to read the answer
Console.ReadKey();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.