【发布时间】:2012-04-06 05:59:15
【问题描述】:
我有一个包含 5 个值的字符串数组,我希望程序循环,即使我没有为数组输入值。如果我在不插入任何东西的情况下拆分数组(按.....(5次“。”拆分数组)那么它不会崩溃它只会循环。但是如果我只是按Enter键,那么程序就会崩溃。
有没有办法修复循环,即使没有输入,它也不会崩溃? (如果您没有完成所有 5 个值,它也会崩溃。)
Net = Console.ReadLine();
string[] oktet = new string[5];
oktet = Net.Split('.', '/');
temp = oktet[0]; //inputs value of array in temp
NaN = int.TryParse(temp, out Net0);
temp = oktet[1];
NaN = int.TryParse(temp, out Net1);
temp = oktet[2];
NaN = int.TryParse(temp, out Net2);
temp = oktet[3];
NaN = int.TryParse(temp, out Net3);
temp = oktet[4];
NaN = int.TryParse(temp, out subnet);
}
while (!NaN | Net0 > 255 | Net0 < 0 | Net1 > 255 | Net1 < 0 | Net2 > 255 | Net2 < 0 | Net3 > 255 | Net3 < 0 | subnet > 32 | subnet < 0);
我知道这很业余,但是嘿,我们是来学习的,对吧? :)
提前致谢!
【问题讨论】:
-
我注意到它缺少一个 do{ 但那是因为我没有在我的复制/粘贴中得到它 ^^
-
不要使用 NaN 等系统名称作为变量名。局部变量名也应该是 camelCase 而不是 PascalCase。 Net 也不是个好名字,改用 ipV4Address。
-
另一个问题是你重新分配了NaN,所以while中的测试只会测试提取subnet的值是否有错误
标签: c# parameters string-parsing