-2
int numRangeFloor;
        switch (hardness)
        {
            case 1:
                numRangeFloor = 5;
                break;
            case 2:
                numRangeFloor = 10;
                break;
            case 3:
                numRangeFloor = 100;
                break;
        }
        Random difficulty = new Random();
        difficulty.Next(0, **numRangeFloor**);

Edit: For those that haven't memorized all the codes:

CS0165 is "Use of unassigned local variable 'name'"

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
L. Rada
  • 1
  • 2

1 Answers1

0

The variable numRangeFloor is not assigned in every case. To the compiler, it's unclear what numRangeFloor is supposed to be if hardness is anything but 1, 2 or 3.

The solution is to either initialize numRangeFloor with a default value or add a default: statement to the switch case.

Here is an example:

int numRangeFloor;
        switch (hardness)
        {
            case 1:
                numRangeFloor = 5;
                break;
            case 2:
                numRangeFloor = 10;
                break;
            case 3:
                numRangeFloor = 100;
                break;
            default:
                throw new Exception();
        }
        Random difficulty = new Random();
        difficulty.Next(0, **numRangeFloor**);
MechMK1
  • 3,278
  • 7
  • 37
  • 55