如何使用C#中的用户条目计算正方形的曲面?

如何使用C#中的用户条目计算正方形的曲面?,c#,readline,surface,square,C#,Readline,Surface,Square,我想写一个程序,用户将被要求的数字,将用于计算一个正方形的表面。但我不知道如何在我的方法中“使用”输入的数字 多谢各位 以下是我迄今为止所做的工作 static void Main(string[] args) { calculate(); } public static void Height() { Console.WriteLine("Enter height of the square"); string enter1 = Console.ReadLine();

我想写一个程序,用户将被要求的数字,将用于计算一个正方形的表面。但我不知道如何在我的方法中“使用”输入的数字

多谢各位

以下是我迄今为止所做的工作

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;
}

让我们首先返回并使用您的输入,以确保您取得一些成功:

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;



    }

}

如果我没有错别字,那么这应该对你有用,但你还有很多东西要学。您应该将I/O操作与您计算的业务逻辑分开,并且为了可重用性和可移植性,应该在两者之间只进行少量通信。我在上面的代码中没有这样做,这是留给您的家庭作业。

让我们先返回并使用您的输入,以确保您取得一些成功:

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;



    }

}

如果我没有错别字,那么这应该对你有用,但你还有很多东西要学。您应该将I/O操作与您计算的业务逻辑分开,并且为了可重用性和可移植性,应该在两者之间只进行少量通信。我没有在上面的代码中这样做,这是留给您的家庭作业。

首先,我建议提取一个方法(读取
double
value):

然后,我们可以实现主例程:

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();
}

首先,我建议提取一个方法(读取
double
value):

然后,我们可以实现主例程:

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();
}

旁注:你可能是指矩形,而不是正方形。另一个旁注:永远不要相信用户的输入。转换可能会失败,导致应用程序崩溃。旁注:你可能是指矩形,而不是正方形。旁注:永远不要相信用户输入。Convert可能会失败,导致你的应用程序崩溃。正如你在对这个问题的评论中所说的:如果宽度不总是等于高度,那么它就不一定是正方形(指变量的命名和提示消息-我知道,我很挑剔;)。编辑后:我建议使用“area”而不是“square”。“抱歉,语法无效;请再试一次”-OP的旁注:信息,有些错误很重要,但并不总是有用的。您可能想告诉用户,有效输入应该是什么样的。正如您在对问题的评论中所述:如果不总是宽度==高度,那么它就不一定是正方形(指变量的命名和提示消息-我知道,我很挑剔;)。编辑后:我建议使用“area”而不是“square”。“抱歉,语法无效;请再试一次”-OP的旁注:信息,有些错误很重要,但并不总是有用的。您可能想告诉用户,有效输入应该是什么样子的。