2017-05-30 25 views
-2

初學者在這裏。想要爲具有一些用戶輸入的圓柱體制作「計算器」。但是,當我運行我的代碼時,我得到一個空白的黑色box。有人能解釋爲什麼我的代碼無法按照原樣運行。C#空控制檯窗口,小型計算器

感謝

 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 
+4

因爲在你寫任何東西之前你從控制檯讀取?嘗試調試,你會看到發生了什麼。 – crashmstr

+0

您的代碼在'Main'中將從上到下運行。每次調用'Console.ReadLine()'都會在「站立」的地方執行。要修復它,把'ReadLines'放在你想要用戶執行輸入的地方。 – Stefan

+0

請參閱:[如何調試小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – EJoshuaS

回答

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。

+0

不用提'Math.PI' – Stefan