repl.it
@kironroy/

csharpquizwithmethods

C#

No description

fork
loading
Files
  • main.cs
  • main.exe
main.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Globalization;

namespace CSharpQuiz
{
    class Program
    {
        static void Main()
        {
            QuizLogic();

        } 

        private static void QuizLogic()
        {
            string myChoice;


            do
            {
                myChoice = QuizPrompt();

                switch (myChoice)
                {
                    case "S":
                        BreakLine();
                        Console.WriteLine("Let's Go!");
                        BreakLine();

                        Dictionary<int, string> quizMessages = QuizPromptsAndAnswers();
                        WelcomeTheQuizTaker(quizMessages);

                        // question 1


                        Console.WriteLine("1. When was C# created? (Type a year)");
                        var quizQuestion1 = Console.ReadLine().Trim();


                        if (quizQuestion1 == "2000")
                        {
                            Console.WriteLine(quizMessages[2]);
                        }
                        else
                            Console.WriteLine(quizMessages[3]);
                        BreakLine();

                        // question 2


                        Console.WriteLine("2. What is the first name of C#'s creator");
                        var quizQuestion2 = Console.ReadLine().Trim().ToLower();


                        if (quizQuestion2 == "anders")
                        {
                            Console.Write(quizMessages[2]);
                            BreakLine();
                        }
                        else
                            Console.WriteLine(quizMessages[3]);
                        BreakLine();


                        // question 3


                        Console.WriteLine("True or false: is C# compiled?, type true or false");
                        var quizQuestion3 = Console.ReadLine().Trim().ToLower();
                        if (quizQuestion3 == "true")
                        {
                            Console.WriteLine(quizMessages[2]);
                        }
                        else
                            Console.WriteLine(quizMessages[3]);
                        BreakLine();

                        Console.WriteLine(quizMessages[4]);
                        break;
                    case "Q":
                        Console.WriteLine();
                        Console.WriteLine(" ~*~ Keep practicing! ~*~");
                        break;
                    default:
                        Console.WriteLine("{0} is not a valid choice", myChoice);
                        break;

                } // do's end bracket
            } while (myChoice != "Q"); // users quits

            Console.ReadKey();
        }

        private static void WelcomeTheQuizTaker(Dictionary<int, string> quizMessages)
        {
            Console.WriteLine("What is your name: ");
            var nameInput = Console.ReadLine().Trim().ToLower();
            var nameInputCaps = new CultureInfo("en-US").TextInfo.ToTitleCase(nameInput);
            BreakLine();
            Console.WriteLine($"Hello {nameInputCaps} {quizMessages[1]}");
        }

        private static Dictionary<int, string> QuizPromptsAndAnswers()
        {
            Dictionary<int, string> quizMessages = new Dictionary<int, string>();
            quizMessages.Add(1, "good luck with the quiz! ");
            quizMessages.Add(2, "Correct!\n");
            quizMessages.Add(3, "Wrong!\n");
            quizMessages.Add(4, " Quiz answers:\n\n 1. 2002\n 2. Anders Hejlsberg\n 3. True, C# is  compiled");
            return quizMessages;
        }

        private static string QuizPrompt()
        {
            string myChoice;
            BreakLine();
            Console.WriteLine("Welcome to C# quiz");
            BreakLine();
            Console.WriteLine("S - Start Quiz");
            Console.WriteLine("Q - Quit\n");
            Console.WriteLine("Choose S or Q: ");
            // Retrieve the user's choice
            myChoice = Console.ReadLine().ToUpper();
            return myChoice;
        }

        private static void BreakLine()
        {
            Console.WriteLine();
        }

    } 

}