File size: 3,346 Bytes
497f2f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.IO;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Console.Clear();
        Console.WriteLine("Input a number of doors to calculate, then press enter");
        StartCalculator();
    }
    
    static void StartCalculator()
    {
        //The number to calculate is input here
        string input = Console.ReadLine();
        Console.Clear();
        
        try
        {
            //The program attempts to convert the string to an int
            //Exceptions will be caught on this line
            int numberOfDoors = Convert.ToInt32(input);
            
            //Will call method recursively if input number is less than 1
            if (numberOfDoors <= 0)
            {
                Console.WriteLine("Please use a number greater than 0");
                StartCalculator();
            }
            
            //The program then starts the calculation process
            Calculate(numberOfDoors);
            
            //After calculation process is finished, restart method is called
            RestartCalculator();
        }
        catch(FormatException)
        {
            //Code will be executed if the number has a decimal or has an unrecognizable symbol
            Console.WriteLine("Unable to read. Please use a real number without a decimal");
            StartCalculator();
        }
        catch (OverflowException)
        {
            //Code will be executed if number is too long
            Console.WriteLine("You number is too long");
            StartCalculator();
        }
    }
    
    static void Calculate(int numberOfDoors)
    {
        //Increases numberOfDoors by 1 since array starts at 0
        numberOfDoors++;
        
        //Dictionary key represents door number, value represents if the door is open
        //if value == true, the door is open
        Dictionary<int, bool> doors = new Dictionary<int, bool>();
        
        //Creates Dictionary size of numberOfDoors, all initialized at false
        for(int i = 0; i < numberOfDoors; i++)
        {
            doors.Add(i, false);
        }
        
        //Creates interval between doors, starting at 0, while less than numberOfDoors
        for (int doorInterval = 0; doorInterval < numberOfDoors; doorInterval++)
        {
            //Will alter every cubby at doorInterval
            //1 needs to be added since doorInterval will start at 0 and end when equal to numberOfDoors
            for(int i = 0; i < numberOfDoors; i += doorInterval + 1)
            {
                //Changes a false value to true and vice versa
                doors[i] = doors[i] ? false: true;
            }
        }
        
        //Writes each door and whether it is open or closed
        for(int i = 0; i < numberOfDoors; i++)
        {
            //Skips over door 0
            if (i == 0) continue;
            //Writes open if door value is true, writes closed if door value is false
            Console.WriteLine("Door " + (i) + " is " + (doors[i] ? "open" : "closed"));
        }
    }
    
    static void RestartCalculator()
    {   
        Console.WriteLine("Press any key to restart");
        Console.ReadKey(true);
        Main();
    }
}