我有一个计算多个数字的阶乘的程序。这些数字在 cmd 中作为参数传递,如下所示:
factorial.exe 3 4 5
这将分别计算 3、4 和 5 的阶乘。 该程序的早期版本有一个百分比来显示堆栈的填充度。我现在想恢复它,但我还将等待时间作为 cmd 中的参数传递,如下所示:
factorial.exe 3 4 5 wait_time1
或
factorial.exe 3 4 5 1000
要读取数字,我使用这个参数解析器:
static string the_time;
public static void Main(string[] args)
{
foreach (string s in args)
{
extra = int.Parse(s);
Calculate(extra);
}
}
如何分离参数? 提前致谢。
最佳答案
您可以添加 waittime
参数,就像开关 /t:100
这样,只有当您看到 /t
时,您才知道它是 等待时间
。
如果您知道您的参数将始终具有waittime
,那么waittime
就是
waittime = arg[args.Length-1]
可能也值得一看
如果您不想重新发明轮子
关于C# 分隔 args 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11772561/