Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

There is a Timer class Timer class, which I have never used. (I am a beginner myself, mind you). There is also System.Diagnostics.StopWatch, which as you can tell is used for diagnostics.

There is a Timer class, which I have never used. (I am a beginner myself, mind you). There is also System.Diagnostics.StopWatch, which as you can tell is used for diagnostics.

There is a Timer class, which I have never used. (I am a beginner myself, mind you). There is also System.Diagnostics.StopWatch, which as you can tell is used for diagnostics.

fixed bug
Source Link
asibahi
  • 937
  • 6
  • 16
class MainClass
{
 public static void Main ()
 {
 var rand = new Random ();
 Console.Write ("How long do want to play in seconds?");
 intvar timerts = TimeSpan.FromSeconds(RequestInput ()); // type is System.TimeSpan
 int correctCount = 0;
 int questionCount = 0;
 var sw = Stopwatch.StartNew ();
 while (sw.Elapsed.Seconds < timerts)
 {
 // .. same old
 questionCount++;
 }
 sw.Stop ();
 Console.WriteLine ($"You got {correctCount} of {questionCount} correct in {sw.Elapsed.Seconds} seconds.!");
 Console.ReadKey ();
 }
}

Edit: i realized there was a bug in my original implementation because I didn't understand how the TimeSpan struct works. I think it should work as expected now for longer times that 1 minute. Make sure to check the MSDN documentation

class MainClass
{
 public static void Main ()
 {
 var rand = new Random ();
 Console.Write ("How long do want to play in seconds?");
 int timer = RequestInput ();
 int correctCount = 0;
 int questionCount = 0;
 var sw = Stopwatch.StartNew ();
 while (sw.Elapsed.Seconds < timer)
 {
 // .. same old
 questionCount++;
 }
 sw.Stop ();
 Console.WriteLine ($"You got {correctCount} of {questionCount} correct in {sw.Elapsed.Seconds} seconds.!");
 Console.ReadKey ();
 }
}
class MainClass
{
 public static void Main ()
 {
 var rand = new Random ();
 Console.Write ("How long do want to play in seconds?");
 var ts = TimeSpan.FromSeconds(RequestInput ()); // type is System.TimeSpan
 int correctCount = 0;
 int questionCount = 0;
 var sw = Stopwatch.StartNew ();
 while (sw.Elapsed < ts)
 {
 // .. same old
 questionCount++;
 }
 sw.Stop ();
 Console.WriteLine ($"You got {correctCount} of {questionCount} correct in {sw.Elapsed}.!");
 Console.ReadKey ();
 }
}

Edit: i realized there was a bug in my original implementation because I didn't understand how the TimeSpan struct works. I think it should work as expected now for longer times that 1 minute. Make sure to check the MSDN documentation

consistency
Source Link
asibahi
  • 937
  • 6
  • 16
class MainClass
{
 public static void Main ()
 {
 var rand = new Random ();
 Console.Write ("How many questions would you like to answer? ");
 int correctCount = 0;
 int questionCount = RequestInput ();
 for (int i = 0; i < questionCount; i++)
 {
 int num01 = rand.Next (11);
 int num02 = rand.Next (11);
 Console.Write ($"What is {num01} times {num02} ?");
 int playerAnswer = RequestInput ();
 if (num01 * num02 == playerAnswer)
 {
 Console.WriteLine ($"{playerAnswer} is correct!");
 correctCount++;
 }
 else
 {
 Console.WriteLine ($"{playerAnswer} is incorrect!");
 }
 }
 Console.WriteLine ($"You got {correctCount} of {questionCount} correct.!");
 Console.ReadKey ();
 }
 static int RequestInput ()
 {
 int playerAnswer;
 string input = Console.ReadLine ();
 while (!int.TryParse (inputConsole.ReadLine (), out playerAnswer))
 {
 Console.WriteLine ("Invalid Input. Please type a number using digits");
 input = Console.ReadLine ();
 }
 return playerAnswer;
 }
}
class MainClass
{
 public static void Main ()
 {
 var rand = new Random ();
 Console.Write ("How many questions would you like to answer? ");
 int correctCount = 0;
 int questionCount = RequestInput ();
 for (int i = 0; i < questionCount; i++)
 {
 int num01 = rand.Next (11);
 int num02 = rand.Next (11);
 Console.Write ($"What is {num01} times {num02} ?");
 int playerAnswer = RequestInput ();
 if (num01 * num02 == playerAnswer)
 {
 Console.WriteLine ($"{playerAnswer} is correct!");
 correctCount++;
 }
 else
 {
 Console.WriteLine ($"{playerAnswer} is incorrect!");
 }
 }
 Console.WriteLine ($"You got {correctCount} of {questionCount} correct.!");
 Console.ReadKey ();
 }
 static int RequestInput ()
 {
 int playerAnswer;
 string input = Console.ReadLine ();
 while (!int.TryParse (input, out playerAnswer))
 {
 Console.WriteLine ("Invalid Input. Please type a number using digits");
 input = Console.ReadLine ();
 }
 return playerAnswer;
 }
}
class MainClass
{
 public static void Main ()
 {
 var rand = new Random ();
 Console.Write ("How many questions would you like to answer? ");
 int correctCount = 0;
 int questionCount = RequestInput ();
 for (int i = 0; i < questionCount; i++)
 {
 int num01 = rand.Next (11);
 int num02 = rand.Next (11);
 Console.Write ($"What is {num01} times {num02} ?");
 int playerAnswer = RequestInput ();
 if (num01 * num02 == playerAnswer)
 {
 Console.WriteLine ($"{playerAnswer} is correct!");
 correctCount++;
 }
 else
 {
 Console.WriteLine ($"{playerAnswer} is incorrect!");
 }
 }
 Console.WriteLine ($"You got {correctCount} of {questionCount} correct.!");
 Console.ReadKey ();
 }
 static int RequestInput ()
 {
 int playerAnswer;
 while (!int.TryParse (Console.ReadLine (), out playerAnswer))
 {
 Console.WriteLine ("Invalid Input. Please type a number using digits");
 }
 return playerAnswer;
 }
}
deleted 142 characters in body
Source Link
asibahi
  • 937
  • 6
  • 16
Loading
added 58 characters in body
Source Link
asibahi
  • 937
  • 6
  • 16
Loading
deleted 9 characters in body
Source Link
asibahi
  • 937
  • 6
  • 16
Loading
Source Link
asibahi
  • 937
  • 6
  • 16
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /