C#有关循环体和goto
……BankClientclientX;a:Console.WriteLine("请输入姓名:");name=Console.ReadLine();switch(name...
……
BankClient clientX;
a:Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
switch (name)
{
case "张三": clientX = clientA; break;
case "李四": clientX = clientB; break;
case "王五": clientX = clientC; break;
case "赵六": clientX = clientD; break;
default: Console.WriteLine("没有这个用户!"); goto a;
}
Console.WriteLine("请输入密码:");
pw = Console.ReadLine();
if (clientX.getBalance(pw) != -1)
{
Console.WriteLine("您的余额为:" + clientX.getBalance(pw));
}
……
部分程序如上
BankClient是之前写好的类,clientA/B/C/D都是这个类的对象
用循环结构的时候会提示“使用了未赋值的局部变量clientX”报错
求不用goto的解决方案 展开
BankClient clientX;
a:Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
switch (name)
{
case "张三": clientX = clientA; break;
case "李四": clientX = clientB; break;
case "王五": clientX = clientC; break;
case "赵六": clientX = clientD; break;
default: Console.WriteLine("没有这个用户!"); goto a;
}
Console.WriteLine("请输入密码:");
pw = Console.ReadLine();
if (clientX.getBalance(pw) != -1)
{
Console.WriteLine("您的余额为:" + clientX.getBalance(pw));
}
……
部分程序如上
BankClient是之前写好的类,clientA/B/C/D都是这个类的对象
用循环结构的时候会提示“使用了未赋值的局部变量clientX”报错
求不用goto的解决方案 展开
展开全部
试试这样:
BankClient clientX = null;
while (null == clientX)
{
Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
switch (name)
{
case "张三": clientX = clientA; break;
case "李四": clientX = clientB; break;
case "王五": clientX = clientC; break;
case "赵六": clientX = clientD; break;
default: Console.WriteLine("没有这个用户!"); break;
}
}
BankClient clientX = null;
while (null == clientX)
{
Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
switch (name)
{
case "张三": clientX = clientA; break;
case "李四": clientX = clientB; break;
case "王五": clientX = clientC; break;
case "赵六": clientX = clientD; break;
default: Console.WriteLine("没有这个用户!"); break;
}
}
展开全部
……
BankClient clientX;
bool flag = false;
while(true)
{
Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
switch (name)
{
case "张三": clientX = clientA; break;
case "李四": clientX = clientB; break;
case "王五": clientX = clientC; break;
case "赵六": clientX = clientD; break;
default: Console.WriteLine("没有这个用户!"); flag=true; break;
}
if(flag == false)
break;
}
Console.WriteLine("请输入密码:");
……
BankClient clientX;
bool flag = false;
while(true)
{
Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
switch (name)
{
case "张三": clientX = clientA; break;
case "李四": clientX = clientB; break;
case "王五": clientX = clientC; break;
case "赵六": clientX = clientD; break;
default: Console.WriteLine("没有这个用户!"); flag=true; break;
}
if(flag == false)
break;
}
Console.WriteLine("请输入密码:");
……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把goto a;
换成
Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
然后再把上面的a:去掉就行了
换成
Console.WriteLine("请输入姓名:");
name = Console.ReadLine();
然后再把上面的a:去掉就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询