2017-05-30 2 views
-2

Anfänger hier. Wollte einen "Taschenrechner" für einen Zylinder mit einigen Benutzereingaben machen. Wenn ich jedoch meinen Code ausführe, erhalte ich ein leeres schwarzes box. Kann jemand erklären, warum mein Code nicht so funktioniert, wie er ist?C# Leeres Konsolenfenster, kleiner Taschenrechner

Dank

 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

Weil Sie von der Konsole lesen, bevor Sie jemals etwas schreiben? Versuchen Sie das Debuggen und Sie werden sehen, was passiert. – crashmstr

+0

Ihr Code in "Main" wird von oben nach unten ausgeführt. Jeder Aufruf von 'Console.ReadLine()' wird an der Stelle ausgeführt, an der er "steht". Um es zu beheben, setzen Sie die 'ReadLines' an den Stellen, an denen der Benutzer Eingaben vornehmen soll. – Stefan

+0

Bitte beachten Sie: [Wie kleine Programme zu debuggen] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – EJoshuaS

Antwort

1

Sie erhalten die Benutzereingabe, bevor die Nachrichten zu drucken. Die richtige Strömung ist wie folgt:

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 

Auch nicht Ihr eigenes definierten pi verwenden. Verwenden Sie stattdessen Math.PI.

+0

Gut zu erwähnen 'Math.PI' – Stefan