1
只需用简单的计算器进行练习。我尝试着跑到前面,并将其封装在do-while循环中。然后我有一个奇怪的行为 - catchin每个新循环的空字符串。可以看到,在case ""
的代码部分。从stdio读取输入时不需要的空字符串
所以这个问题 - 正在发生什么,以及如何处理它?
可以看到我的谦卑尝试注释行修复它在代码:
import std.stdio;
import std.string;
void main() {
writefln("--- Welcome to calculatro %s ---", " ");
int exit = 0;
do {
string op;
double first;
double second;
writeln("enter operator :");
op = chomp(readln());
writeln("operator :",op,":");
//readf(" %s/n", &op);
switch (op) {
case "add", "+":
writeln("enter two values :");
//readf(" %s %s", &first, &second);
readf(" %s", &first);
readf(" %s", &second);
writefln("%s+%s=%s", first, second, first+second);
//writeln(first+second);
break;
case "minus", "-", "substract":
writeln("enter two values :");
readf(" %s %s", &first, &second);
writefln("%s+%s=%s", first, second, first-second);
break;
case "exit":
exit = 1;
break;
case "":
writeln("empty op");
break;
default:
writefln("i dont know op!"~op);
//writefln("%(%s%)", op);
//writefln("%s", op);
//throw new Exception(format("Unknown operation: %s", op));
break;
}
} while (exit == 0);
writeln("good bye!");
}
非常感谢您的详细解释! – Rijk