- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在用户输入错误的 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/
我最后一次使用C++是在它无法被管理之前。不过最近,我从 Java 回来,看到现在可以管理 C++ 了! 没过多久我就意识到gcnew 和^ 的用途。但是,我对容器有点卡住了。 如何创建一个容器,其元
我离开 Microsoft 堆栈已有一段时间了,专注于 Linux、开源内容和 PHP 中的 Web 开发。我曾经在 Dev Studio(所有 C 和 C++)中在 Windows 上进行一些桌面应
在我的程序中有两个 View Controller 。第一个有表格 View 。当我单击一个单元格时,相关的炎症会显示在第二个 View Controller 中。它运作良好。 当我返回到第一个 Vi
我使用 AVAssetWriter 和 CMSampleBuffer 数据(来自视频、音频输入)录制视频(.mp4 文件)。 在录制时我想处理帧,我正在将 CMSampleBuffer 转换为 CII
在 python 中有两种不同的离开循环的选项。 continue 将您带回到循环的开头,break 就像一个电灯开关,它会在脚本运行的剩余时间内切断循环。我的问题是我有一个 while True 循
我是 Git 的新手,我正试图恢复到 SourceTree 中的先前提交。我右键单击要还原到的提交,然后单击 checkout 。它给了我一个提示,说我的工作副本将成为一个独立的头。这是什么意思,这是
所以我决定在离开几年后,为了我的一些个人项目重新使用 Ruby on Rails。我想知道的是,找出 Rails 中的新功能的最佳资源是什么?自从 1.2 是新的以来,我什至没有真正接触过 Rails
我的项目有两个部分。第一部分是在 Storyboard中制作的,第二部分是 SKView。如何从 SKView 中的第二部分返回到主 UIView? 最佳答案 创建自定义 ViewController
所以我在大约四次提交前对我的项目做了一个糟糕的改变。我了解到我可以恢复到之前描述的状态 here ,并通过依次检查以前的提交(并在我的设备上测试它们),我已经确定了问题发生的位置。 现在我想回到坏改变
我想知道,在 Canvas 的 commandAction 方法中,如何让我的命令按钮回到 MIDlet 的开始? (基本上重新开始)。 当按键触发时,我将它带到一个新的列表页面。在该页面上,我有一个
我想知道是否可以使用 intro.js 返回到下一行。我尝试了\n 和其他类似的东西,但它们中的任何一个都有效并且不可能在文档中找到类似的东西。有谁知道这是否可能? 最佳答案 正确的做法是像这样使用
这是关于我发现我的应用程序面临的一个反复出现的问题,它与使用几个 DialogFragment 相关。我主要针对平台级别 8 设备,因此要使用 DialogFragments,我必须使用兼容性库。 每
我有一个 uiview 的问题,它放置在 Storyboard的一个位置,在应用程序启动后,我将 uiview 移动到第二个位置,并使用代码中的按钮进行动画处理。 int alpha = -212;
我有 Controller B,它使用委托(delegate)模式将数据发送回 Controller A,但由于某种原因我的 segue 没有触发。 是否有什么东西阻止我的 segue 被触发?我将如
我已经找到了处理除我需要的之外的所有内容的解决方案。这是场景 就像在 GMail 中一样 - 主要内容呈现在 iframe 中。单击主页上的链接会指向 iframe。这效果很好,而且无缝。此时,如果我
我有一个 RCP 程序,带有需要登录的启动屏幕。 我想制作一个注销按钮。通过单击此按钮,用户应该返回到初始屏幕,因此他必须重新登录.. 这可能吗? 提前致谢。 最佳答案 如果您使用org.eclips
我有一个数据框: df = pd.DataFrame({'Section': [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6],
是否可以将元素 $("p") 返回到应用 mouseenter() 方法之前的确切颜色?或者我是否需要知道 mouseenter() 之前的颜色,然后使用 mouseleave() 应用该颜色?我希望
在 Matlab R2016b 中,显示某些数据类型的变量会显示有关该类型的信息。当通过不带最终分号键入变量来显示变量时会发生这种情况(使用 disp 函数时不会发生这种情况)。 比较例如: Matl
是否可以告诉 RSpec::Mocks 为一组值 stub 一个方法,否则回退到原始方法?例如: File.stub(:exist?).with(/txt/).and_return(true) Fil
我是一名优秀的程序员,十分优秀!