forked from application-development-08664/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (50 loc) · 1.58 KB
/
Program.cs
File metadata and controls
66 lines (50 loc) · 1.58 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
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
bool keepRunning = true;
while (keepRunning)
{
int first = ReadInteger("Enter first number: ");
int second = ReadInteger("Enter second number: ");
string operation;
double? result = null;
while (true)
{
Console.Write("Choose operation (+, -, *, /, %, = : ");
operation = Console.ReadLine();
Console.WriteLine();
if ("+-*/%=".Contains(operation))
break;
Console.WriteLine("Incorrect Operation Used, please try again");
}
if (operation == "=")
{
keepRunning = false;
Console.WriteLine("Program terminated.");
}
else
{
result = Calculate(first, second, operation);
if (result != null)
Console.WriteLine("Result: " + result);
}
Console.WriteLine();
}
}
public static int ReadInteger(string message)
{
int value;
while (true)
{
Console.Write(message);
string input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input)){
continue;
}
if (int.TryParse(input, out value))
return value;
Console.WriteLine("Invalid input. Please enter a whole number.");
}
}