2017-05-30 10 views
-2

ここは初心者です。いくつかのユーザー入力を持つ円柱のための "電卓"を作りたがっていました。しかし、コードを実行すると空白の黒色になりますbox。誰かが私のコードがそれと同じように機能しない理由を説明することはできますか?C#Emptyコンソールウィンドウ、小電卓

おかげ

 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