사용자가 잘못된 키를 입력하면 여기에 표시된 코드의 처음으로 돌아 가야합니다. 다른 줄로 돌아갈 간단한 코드 줄이 있습니까? 보시다시피 이미 if 문을 설정 했으므로 코드의 처음이나 다른 영역으로 돌아갈 수있는 무언가를 추가 할 수 있습니다. 나는 C#과 프로그래밍 전반에 새로운 기술을 익히고있다. 나는 정말로 똑같은 문제를 일으킬 다른 if 문으로 모든 코드를 다시 입력하고 싶지 않습니다. 나는 사용자가 틀린 키를 입력 한 후에 코드를 다시 실행하는 것이 바람직하다. 처음부터 다시 시작할 필요없이 다시 읽을 수 있기 때문이다. 당신은 몇 가지 옵션이 있습니다코드 블록의 처음으로 돌아 가기?
//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;
}
, 그것은 더 나은하지 않을까요? –
함수에서 코드를 설정하고 사용자가 올바른 키를 누를 때까지 반복해서 호출 할 수 있습니까? 함수가 존재하는 이유는 바로 이것입니다. – Sugar
그 코드를 보여주지 마십시오 (예제와 실제 코드의 함수로 묶음). 어떻게 사용했는지 보여주십시오. –