C#空控制台窗口,小型计算器

时间:2017-05-30 17:56:08

标签: c# calculator

初学者。想要为具有一些用户输入的气缸制作“计算器”。但是,当我运行我的代码时,我得到一个空白的黑色box。有人可以解释为什么我的代码不能按原样运行。

谢谢

        decimal pi = 3.1415926m; // well I guess it's long enough
        string userInputHeight = Console.ReadLine(); // first userInput
        decimal h = Convert.ToDecimal(userInputHeight); // h for height
        string userInputRadius = Console.ReadLine(); // second userInput 
        decimal r = Convert.ToDecimal(userInputRadius); // r for radius
        decimal V = pi * r * 2 * 2 * h; // formular for volumue of a cylinder. Didn't know how to use the '^' sign. So I decided to use 2*2 instead.
        decimal SA = 2 * pi * r * (r + h); // formular for the surface area of the cylinder.

        Console.WriteLine("Welcome to Cylinders! \n Please type first the height of your cylinder and confirm with spacebar " + userInputHeight); // Welcomes the user and asks for the height
        Console.ReadKey(); // waits for user input (in this case spacebar, but it doesn't matter which key is pressed)
        Console.WriteLine("Please type now the radius of your cylinder and confirm with spacebar " + userInputRadius); // Asks the user for the radius
        Console.ReadKey(); // waits for user input (in this case spacebar, but it doesn't matter which key is pressed)
        Console.WriteLine("The volume of your cylinder equals " + V + " and" + " the surface area equals " + SA); // this is where the magic happens.  
        Console.ReadKey(); // waits for user input (in this case spacebar, but it doesn't matter which key is pressed)
        Console.WriteLine("That's it! Press any key to close."); // closes with any key the window

1 个答案:

答案 0 :(得分:1)

您在打印邮件之前收到用户输入。正确的流程如下:

    Console.WriteLine("Welcome to Cylinders! \n Please type first the height of your cylinder and confirm with spacebar " + userInputHeight); // Welcomes the user and asks for the height
    string userInputHeight = Console.ReadLine(); // first userInput
    decimal h = Convert.ToDecimal(userInputHeight); // h for height
    Console.WriteLine("Please type now the radius of your cylinder and confirm with spacebar " + userInputRadius); // Asks the user for the radius  
    string userInputRadius = Console.ReadLine(); // second userInput 
    decimal r = Convert.ToDecimal(userInputRadius); // r for radius
    decimal V = Math.PI * r * 2 * 2 * h; // formular for volumue of a cylinder. Didn't know how to use the '^' sign. So I decided to use 2*2 instead.
    decimal SA = 2 * Math.PI * r * (r + h); // formular for the surface area of the cylinder.
    Console.WriteLine("The volume of your cylinder equals " + V + " and" + " the surface area equals " + SA); // this is where the magic happens.  
    Console.ReadKey(); // wait's for user input (in this case spacebar, but it doesn't matter which key is pressed)
    Console.WriteLine("That's it! Press any key to close."); // closes with any key the window

另外,不要使用自己定义的pi。请改用Math.PI。