You will notice I used Stopwatch.StartNew() which is a static method that creates a new instance of Stopwatch and starts it immediately. static int RequestInput ()Ĭonsole.Write ("How many questions would you like to answer? ") Ĭonsole.Write ($"What is. I am not very imaginative so I just called it RequestInput. It does all the thinking for you and it is fantastic. or the small light bulb to the side, and selecting, well, Extract Method. You can easily do that in Visual Studio by selecting the code you want to extract into a method, pressing Ctrl +. Instead of typing it twice, you should extract it into a method. ![]() When you ask for the number of questions and when you ask for the answer. Something of note: you need this little piece of code in two places. While (!int.TryParse (Console.ReadLine (), out playerAnswer))Ĭonsole.WriteLine ("Invalid Input. If you want to keep asking for input until you get something valid, you can use a while loop : int playerAnswer If (int.TryParse (Console.ReadLine (), out playerAnswer)) The syntax is a bit weird and takes some getting used to, but it is being made nicer in C# 7.0 (not released yet). TryParse returns a bool to indicate whether the conversion is successful or not, and has an out variable which assigns a value to an already declared variable. The trick is to use int.TryParse to parse inputs. Obviously if you're shipping the app (I know you're not, but humor me), you can't have that. Your code crashes when I type something that is NOT a number. I am sure you have seen something that looks exactly like this in your readings. (It also standard practice to increment rather decrement). So this : int questionCount= Convert.ToInt32 (Console.ReadLine ()) Ĭan be better written as: int questionCount= Convert.ToInt32 (Console.ReadLine ()) įor (int questionLeftCount = questionCount questionLeftCount > 0 questionLeftCount-)Īlso, since questionLeftCount is only used within the for loop definition, just use i or iter for iterator. Using a for loopįor loops are easier for humans to process than while loops, I think. I also like questionCount more than numOfQuestions. (A crude example, but I am sure you have seen the photo where KidsExchange reads like KidSexChange due to uniform capitalization KIDSEXCHANGE.) Again, it doesn't really matter for such a short app but for bigger apps such practices are a life saver. You don't have any methods defined but you need to know the convention. So, in your code: playeranswer becomes playerAnswer, and so on. Net convention is to use PascalCase for methods and classes and the like, and camelCase for variables. ![]() I think it is better to have declaration and assignment in one line, it is terser and more readable: ( int numofquestions = Convert.ToInt32(Console.ReadLine()) ). So it is better to declare then inside the loop (or you might accidentally use them outside or simply crowd your intellisense).Īlso, for some variables you're splitting declaration ( int numofquestions ) and assignment ( numofquestions = Convert.ToInt32(Console.ReadLine()) ), and for other you're not. But as your application gets bigger and better you don't want too many variables in scope.įrom your code: num01, num02, playeranswer, and answer are only used within the loop. This is to decrease variable pollution in bigger functions. While it might be more performance friendly to declare all variables from the outset, it is best practice, I think, to declare variables only within the scope they're used. I got some comments, some of which are my personal opinion (for example how to declare variables), some are accepted conventions (naming), and some are bug fixes. Letting the user know how many answers they got right.Ĭonsole.WriteLine("You got " + numofcorrect + " of " + numofquestions + " correct!") Playeranswer = Convert.ToInt32(Console.ReadLine()) Ĭonsole.WriteLine(playeranswer + " is correct!") Ĭonsole.WriteLine(playeranswer + " is incorrect! Try again.") Answering a question correctly increases your score.Ĭonsole.Write("What is " + num01 + " times " + num02 + "? ") This is the loop which handles the actual question/answer core of the game. Numofquestions = Convert.ToInt32(Console.ReadLine()) int timer = 30 I considered a timer function but that ended up being way over my head.Ĭonsole.Write("How many questions would you like to answer? ") It does work as intended, and I'm really just fishing for someone to yell at me for doing something glaringly bad practice in the code below. ![]() I am only a beginner when it comes to programming, but after reading tons of tutorials on and offline, I have been able to create this simple math quiz.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |