on master help sign in
ikoHSE/KR1/KR/iko.cs
64 lines 2.0 kB history raw
1using System;
2using static System.Console;
4namespace Iko {
5 static class Console {
6 public static double GetDouble(string text, Func<double, bool> validate, string falseText) {
7 Write(text);
8 double res;
9 while (true) {
10 if (double.TryParse(ReadLine(), out res) && validate(res)) {
11 return res;
12 } else {
13 WriteLine(falseText);
14 Write(text);
15 }
16 }
17 }
19 public static double GetDouble(string text, string falseText) {
20 return GetDouble(text, _ => { return true; }, falseText);
21 }
23 public static float GetFloat(string text, Func<float, bool> validate, string falseText) {
24 Write(text);
25 float res;
26 while (true) {
27 if (float.TryParse(ReadLine(), out res) && validate(res)) {
28 return res;
29 } else {
30 WriteLine(falseText);
31 Write(text);
32 }
33 }
34 }
36 public static float GetFloat(string text, string falseText) {
37 return GetFloat(text, _ => { return true; }, falseText);
38 }
40 public static int GetInt(string text, Func<int, bool> validate, string falseText) {
41 Write(text);
42 int res;
43 while (true) {
44 if (int.TryParse(ReadLine(), out res) && validate(res)) {
45 return res;
46 } else {
47 WriteLine(falseText);
48 Write(text);
49 }
50 }
51 }
53 public static int GetInt(string text, string falseText) {
54 return GetInt(text, _ => { return true; }, falseText);
55 }
57 public static void Repeat(Action f) {
58 do {
59 f();
60 Write("Press Enter to repeat program. Press any other key to quit.");
61 } while (ReadKey().Key == ConsoleKey.Enter);
62 }
63 }