gpt4 book ai didi

c# - 回到代码块的开头?

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:50 25 4
gpt4 key购买 nike

在用户输入错误的 key 后,我需要返回到此处显示的代码的开头。是否有任何简单的代码行会返回到另一行?如您所见,我已经设置了一个 if 语句,所以我可以添加一些可以返回到代码开头或另一个区域的内容。一般来说,我对 C# 和编程真的很陌生。我真的只是不想将所有代码再次输入到另一个会产生相同问题的 if 语句中。我希望让代码在用户输入错误的 key 后再次运行,因为这样他们就可以重新阅读代码,而不必再次从头开始。

//Runs battle interactive
Console.WriteLine("");
Console.WriteLine("You have encountered a simple guard! He deals 2 damage per attack and has 1 HP.");
Console.WriteLine("You currently have: " + Program.Inventory);
Console.WriteLine("Choose a weapon!");
var input2 = Console.ReadKey();


//Key checker for items
switch (input2.Key)
{
case ConsoleKey.D1:
Console.WriteLine("");
if (Items.iniFists == true)
{
Console.WriteLine("You have attacked with your Fists for 1 DMG!");
}
else
{
//this will never run, just a placeholder
Console.WriteLine("You Don't have your fists!");
switch (input2.Key)
{
case ConsoleKey.D1:
Console.WriteLine("");
if (Items.iniFists == true)
{
Console.WriteLine("You have attacked with your Fists for 1 DMG!");
}
else
{
//this will never run, just a placeholder
Console.WriteLine("You Don't have your fists!");
}
break;
case ConsoleKey.D2:
Console.WriteLine("");
if (Items.iniLongsword == true)
{
Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
}
else
{
Console.WriteLine("You don't have a longsword!");
}
break;
case ConsoleKey.D3:
Console.WriteLine("");
if (Items.iniBow == true)
{
Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
}
else
{
Console.WriteLine("You don't have a Bow!");
}
break;
case ConsoleKey.D4:
Console.WriteLine("");
if (Items.iniLightstaff == true)
{
Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
}
else
{
Console.WriteLine("You don't have a Lightstaff!");
}
break;
case ConsoleKey.D5:
Console.WriteLine("");
Console.WriteLine("You can't attack with an Apple!");
break;
case ConsoleKey.D6:
Console.WriteLine("");
Console.WriteLine("You can't attack with a Golden Key!");
break;
case ConsoleKey.D7:
Console.WriteLine("");
Console.WriteLine("You can't attack with a Steak!");
break;
}
}
break;
case ConsoleKey.D2:
Console.WriteLine("");
if (Items.iniLongsword == true)
{
Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
}
else
{
Console.WriteLine("You don't have a longsword!");
}
break;
case ConsoleKey.D3:
Console.WriteLine("");
if (Items.iniBow == true)
{
Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
}
else
{
Console.WriteLine("You don't have a Bow!");
}
break;
case ConsoleKey.D4:
Console.WriteLine("");
if (Items.iniLightstaff == true)
{
Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
}
else
{
Console.WriteLine("You don't have a Lightstaff!");
}
break;
case ConsoleKey.D5:
Console.WriteLine("");
Console.WriteLine("You can't attack with an Apple!");
break;
case ConsoleKey.D6:
Console.WriteLine("");
Console.WriteLine("You can't attack with a Golden Key!");
break;
case ConsoleKey.D7:
Console.WriteLine("");
Console.WriteLine("You can't attack with a Steak!");
break;
}

最佳答案

C# 支持代码中的标签,但不推荐使用,因为它违反了许多编码最佳实践,但我想任何规则总会有异常(exception)。

class Program
{
static void Main(string[] args)
{
Start:
Console.WriteLine("Start Here... Press any key");
var key = Console.ReadKey(true);

switch (key.Key)
{
case ConsoleKey.A:
goto MyLabel;

case ConsoleKey.B:
goto MyLabel2;

case ConsoleKey.C:
goto MyLabel3;

default:
Console.WriteLine("Bad Choice");
goto Start;

}

MyLabel:
Console.WriteLine("MyLabel: A");
goto Start;

MyLabel2:
Console.WriteLine("MyLabel: B");
goto Start;


MyLabel3:
Console.WriteLine("MyLabel: C");
goto Start;
}
}

您可以在此处找到更多信息:

http://msdn.microsoft.com/en-us/library/d96yfwee.aspx http://msdn.microsoft.com/en-us/library/13940fs2.aspx

关于c# - 回到代码块的开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20722263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com