A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
34
Task/Menu/D/menu.d
Normal file
34
Task/Menu/D/menu.d
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import std.stdio, std.conv, std.string, std.array;
|
||||
|
||||
string menuSelect(in string[] entries) {
|
||||
static int validChoice(in string input,
|
||||
in int nEntries) pure nothrow {
|
||||
try {
|
||||
immutable n = to!int(input);
|
||||
return (n >= 0 && n <= nEntries) ? n : -1;
|
||||
} catch (Exception e) // very generic
|
||||
return -1; // not valid
|
||||
}
|
||||
|
||||
if (entries.empty)
|
||||
return "";
|
||||
|
||||
while (true) {
|
||||
writeln("Choose one:");
|
||||
foreach (i, const string entry; entries)
|
||||
writefln(" %d) %s", i, entry);
|
||||
writef("> ");
|
||||
immutable input = readln().chomp();
|
||||
immutable choice = validChoice(input, entries.length-1);
|
||||
if (choice != -1)
|
||||
return entries[choice]; // we have a valid choice
|
||||
else
|
||||
writeln("Wrong choice.");
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable items = ["fee fie", "huff and puff",
|
||||
"mirror mirror", "tick tock"];
|
||||
writeln("You chose '", menuSelect(items), "'.");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue