如何在.net控制台应用程序中设置默认输入值? [英] How can you set a default input value in a .net console app?
本文介绍了如何在.net控制台应用程序中设置默认输入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
如何在.net控制台应用程序中设置默认输入值?
tx
Hi all
How can you set a default input value in a .net console app?
tx
推荐答案
例如:
const byte defaultInput = 4;
const byte inputMin = 1;
const byte inputMax = 8;
//...
byte input = defaultInput; //this answers the question, see below
System.Console.Write("Enter value ({0}..{1}); for default value ({2}), press ENTER: ",
inputMin, inputMax, defaultInput); //updated to show default
string line = System.Console.ReadLine().Trim();
if (line.Length > 0) { // if not (trimmed string is empty) default value remains
try {
input = byte.Parse(line); //may throw exception
if (input < inputMin || input > inputMax)
//deal with invalid range
} catch (/*...*/) {
//deal with invalid input format
}
}
这篇关于如何在.net控制台应用程序中设置默认输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文