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
Weil Sie von der Konsole lesen, bevor Sie jemals etwas schreiben? Versuchen Sie das Debuggen und Sie werden sehen, was passiert. – crashmstr
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
Bitte beachten Sie: [Wie kleine Programme zu debuggen] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – EJoshuaS