static void Main(string[] args)
{
string temp;
do
{
temp = Console.ReadLine();
} while (string.IsNullOrEmpty(temp)); //如果不合法一直循环要求输入(此处用的条件是如果用户输入的为空或者是null)
Console.WriteLine("你的输入正确!!!");
Console.ReadLine();
}
你可以参照下上面的代码试试!
if(不合法)
{
如果不合法时要执行的操作;
return;
}
合法操作
string a = "abc"; // 条件
string b;
bool flag;
input:
{
Console.WriteLine("input the password:"); //输入条件“abc”
b = Console.ReadLine().ToString();
if (a == b)
{
flag = true;
Console.WriteLine("passed.");
}
else
{
Console.WriteLine("error, input again:");
flag = false;
}
}
if (flag ==false) { goto input; }
Console.ReadKey();
if(不合法)
{
初始化界面
return;
}
else
{
合法操作
}
if(!=)
return flase
return true
因为一直要等到输入合法后,才退出判断合法的语句,所以要使用循环语句.
循环语句需要一个标志来告诉程序什么时候结束循环.所以使用布尔型testEnter来作为标志位.当testEnter为真的时候,就认为是输入合法了,为假的时候输入就不合法.它的初始值设置为假.所以代码如下
//循环判断标志,初始为假
bool testEnter =false;
//循环开始,当标志位(testEnter为假时循环)
while(testEnter == false)
{
//写入你获得输入的内容
//判断是否合法
if(合法)
{
//合法情况的处理
//标志位设置为真
testEnter == true;
}
else
{
//不合法情况的处理
}
}
最后,由于你给的判断输入是否合法的条件不明确,所以只能用中文表示,无法写出具体的关于合法性判断的代码