0

I am making a console maths test that asks a few random addition, subtraction, multiplication, division, power and square-root questions based on the difficulty level the user chooses!

Here is my code:

using System;
using System.Collections.Generic;

namespace mathstester
{
    class Program
    {
        public enum UserDifficulty
        {
            Easy,
            Normal,
            Hard
        }

        public static void Main(string[] args)
        {
            Dictionary<string, UserDifficulty> difficultyDictionary = new Dictionary<string, UserDifficulty>();
            difficultyDictionary.Add("E", UserDifficulty.Easy);
            difficultyDictionary.Add("N", UserDifficulty.Normal);
            difficultyDictionary.Add("H", UserDifficulty.Hard);

            string userInputDifficulty;
            do
            {
                Console.WriteLine("What difficulty level would you like to do! Please type E for Easy, N for Normal and H for hard");
                userInputDifficulty = Console.ReadLine().ToUpper();
            } while (userInputDifficulty != "E" && userInputDifficulty != "N" && userInputDifficulty != "H");

            UserDifficulty userDifficulty = difficultyDictionary[userInputDifficulty];
        }
    }
}

However, I am trying to make my code more advanced by automatically setting the difficulty to the harder level or easier level the next time he runs the code depending on what score he got the last time he did the test. For example, if he chose the Normal level and gets a score of 1/10, the next time he runs the code he should automatically be doing the easy level.

But I don't know where to start.

It would really help if someone could show me how I would begin!

Btw if you can think of a better title, please edit it!

d51
  • 316
  • 1
  • 6
  • 23
  • 6
    This is a bit broad. But at a high level, the key to what you're asking is right here: *"the next time he runs the code depending on what score he got the last time"* - If you want to store information outside the context of the application itself then you need to store it somewhere. A database, a file, etc. It's up to you where you want to store it. Once you decide that and start writing your value, the next step would be to have your application read the value when it starts. (Don't forget to handle the case where no value is found.) – David Apr 21 '20 at 12:27
  • In addition to David, you could even store it as an environment variable and read that back next time the application is run. – mattbloke Apr 21 '20 at 12:33

1 Answers1

1

There are a bunch of ways to store data for a program, some examples:

  • Registry
  • Environment variable
  • File
  • Database

One fairly easy way is to use application settings built into visual studio. Go to the properties page of your project and the settings view. Add a new setting, give it a name (like Difficulty), int type, and user scope. You will need to cast it to/from your enum unless you want to add it as a type explicitly.

var storedSetting = (UserDifficulty)Properties.Settings.Default.Difficulty;
Properties.Settings.Default.Difficulty= (int)UserDifficulty.Easy;
Properties.Settings.Default.Save();
JonasH
  • 28,608
  • 2
  • 10
  • 23
  • I can't find my properties page. Where is it on mac? – d51 Apr 21 '20 at 23:23
  • I'm not sure this application settings is available on mac. An alternative would be to use a json file. See this answer for details: https://stackoverflow.com/a/6541739/12342238 – JonasH Apr 22 '20 at 09:42