forked from TheCSharpAcademy/CodeReviews.Console.MathGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (86 loc) · 4.1 KB
/
Program.cs
File metadata and controls
93 lines (86 loc) · 4.1 KB
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
string userInput = "";
List<string> operationHistory = new List<string>();
do
{
Console.WriteLine("=== Welcome to Math Game ===");
Console.WriteLine("Select an operation:");
Console.WriteLine("1. Addition (a + b)");
Console.WriteLine("2. Subtraction (a - b)");
Console.WriteLine("3. Multiplication (a * b)");
Console.WriteLine("4. Division (a / b, result must be integer, dividend ≤ 100)");
Console.WriteLine("5. View operation history");
Console.WriteLine("Type 'Exit' to quit the game.");
Console.Write("Your choice: ");
userInput = Console.ReadLine();
string logUserHistory;
switch (userInput)
{
case "1":
Console.Write("Enter two numbers separated by a comma (e.g., 5,3): ");
var additionInput = Console.ReadLine();
string[] additionValues = additionInput.Split(",");
var additionOutput = int.Parse(additionValues[0]) + int.Parse(additionValues[1]);
Console.WriteLine($"Addition Result: {additionValues[0]} + {additionValues[1]} = {additionOutput}");
logUserHistory = $"{additionValues[0]} + {additionValues[1]} = {additionOutput}";
operationHistory.Add(logUserHistory);
break;
case "2":
Console.Write("Enter two numbers separated by a comma (e.g., 5,3): ");
var subtractionInput = Console.ReadLine();
string[] subtractionValues = subtractionInput.Split(",");
int subtractionOutput = int.Parse(subtractionValues[0]) - int.Parse(subtractionValues[1]);
Console.WriteLine($"Subtraction Result: {subtractionValues[0]} - {subtractionValues[1]} = {subtractionOutput}");
logUserHistory = $"{subtractionValues[0]} - {subtractionValues[1]} = {subtractionOutput}";
operationHistory.Add(logUserHistory);
break;
case "3":
Console.Write("Enter two numbers separated by a comma (e.g., 5,3): ");
var multiplicationInput = Console.ReadLine();
string[] multiplicationValues = multiplicationInput.Split(",");
var multiplicationOutput = int.Parse(multiplicationValues[0]) * int.Parse(multiplicationValues[1]);
Console.WriteLine($"Multiplication Result: {multiplicationValues[0]} * {multiplicationValues[1]} = {multiplicationOutput}");
logUserHistory = $"{multiplicationValues[0]} * {multiplicationValues[1]} = {multiplicationOutput}";
operationHistory.Add(logUserHistory);
break;
case "4":
Console.WriteLine("Division: The dividend must be ≤ 100 and result must be an integer.");
Console.Write("Enter the divisor: ");
int divisorInput = int.Parse(Console.ReadLine());
Console.Write("Enter the dividend: ");
int dividendInput = int.Parse(Console.ReadLine());
if (divisorInput == 0)
{
Console.WriteLine("Error: Division by zero is not allowed.");
break;
}
if (dividendInput > 100)
{
Console.WriteLine("Error: The dividend must be 100 or less.");
break;
}
if (dividendInput % divisorInput != 0)
{
Console.WriteLine("Error: The result is not an integer. Please try again.");
break;
}
var divisionOutput = dividendInput / divisorInput;
Console.WriteLine($"Division Result: {dividendInput} / {divisorInput} = {divisionOutput}");
logUserHistory = $"{dividendInput} / {divisorInput} = {divisionOutput}";
operationHistory.Add(logUserHistory);
break;
case "5":
Console.WriteLine("=== Operation History ===");
if (operationHistory.Count == 0)
{
Console.WriteLine("No operations performed yet.");
}
else
{
foreach (string history in operationHistory)
{
Console.WriteLine(history);
}
}
break;
}
} while (userInput != "Exit");