-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrueOrFalse.cs
89 lines (69 loc) · 2.67 KB
/
TrueOrFalse.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
using System;
namespace TrueOrFalse
{
class Program
{
static void Main(string[] args)
{
// Do not edit these lines
Console.WriteLine("Welcome to 'True or False?'\nPress Enter to begin:");
string entry = Console.ReadLine();
Tools.SetUpInputStream(entry);
// Type your code below
string[] questions = new string[]
{
"An eggplant is also known as an aubergine.",
"Eggplants are a species in the nightshade family.",
"According to botanical definition, eggplant is a vegetable.",
"When cut open, eggplants do not brown (oxidation)",
"Eggplants were originally domesticated from the wild species bitter apple, or S. inconum."};
// The correct answers
bool[] answers = new bool[] { true, true, false, false, true };
RunQuiz(questions, answers);
}
static void RunQuiz(string[] questions, bool[] answers)
{
// The user replies
bool[] responses = new bool[questions.Length];
// Checking if we have as many answers as questions
if (questions.Length != answers.Length)
{
Console.WriteLine("You need as many answers as questions!");
}
// Ask the questions and store the user replies
int askingIndex = 0;
foreach (string i in questions)
{
string input;
bool isBool;
bool inputBool;
Console.WriteLine(questions[askingIndex]);
Console.WriteLine("True or false?");
input = Console.ReadLine();
isBool = Boolean.TryParse(input, out inputBool);
while (!isBool)
{
Console.WriteLine("Please respond with 'true' or 'false'.");
input = Console.ReadLine();
isBool = Boolean.TryParse(input, out inputBool);
}
responses[askingIndex] = Convert.ToBoolean(input);
askingIndex++;
}
//Calculate score
int scoringIndex = 0;
int score = 0;
foreach (bool answer in answers)
{
bool response = responses[scoringIndex];
Console.WriteLine($"{scoringIndex + 1}. Input: {response} | Answer: {answer}");
if (response == answer)
{
score++;
}
scoringIndex++;
}
Console.WriteLine($"You got {score} ot of {answers.Length} correct!");
}
}
}