why won't this C# code run in the terminal?: using System; namespace Prog; class…
- User
why won't this C# code run in the terminal?: using System;
namespace Prog;
class GuessGame { static void Main(string[] args) {
}
void Rolling() { Random r = new Random(); int res = r.Next( 45); Console.WriteLine("Roll the Dice!"); String? Roll = Console.ReadLine();
codeif (Roll == "Roll") { Console.WriteLine(res); } Console.ReadKey();}
}
- Assistant
There are multiple issues with this C# code that prevent it from running in the terminal:
-
The namespace should not contain a semicolon. Instead of
namespace Prog;, it should benamespace Prog. -
The
Rollingmethod is not being called from theMainmethod. You need to either callRollingfromMainor move the code fromRollingdirectly intoMain. -
String?is not valid C#. It should bestring(lowercase).
Here's the corrected code:
code -
- User
Look at this C# code, how do I make the code loop itself where it always awaits the users input to "Roll the Dice!": using System;
namespace Prog;
class GuessGame { static void Main(string[] args) {
codeGuessGame g = new GuessGame(); g.Game(); Console.ReadKey();}
void Game() { Random r = new Random(); int res = r.Next( 45); Console.WriteLine("Roll the Dice!"); String Roll = Console.ReadLine();
codeif (Roll == "Roll") { Console.WriteLine(res); }}
}
- Assistant
To make the code loop so that it constantly awaits the user's input to "Roll the Dice!", you can use a
whileloop that keeps running until the user enters something other than "Roll". Here's the modified code:codeusing System; namespace Prog { class GuessGame { static void Main(string[] args) { GuessGame g = new GuessGame(); g.Game(); Console.ReadKey(); } void Game() { Random r = new Random(); while (true) { Console.WriteLine("Roll the Dice!"); string Roll = Console.ReadLine(); if (Roll == "Roll") { int res = r.Next(45); Console.WriteLine(res); } else { break; } } } } }