我怎样才能告诉我的程序“重新做”?不同的动作(由用户输入选择)完成后,直到do-while需求设置
本文关键字:用户 输入 选择 do-while 需求 设置 直到 我的 程序 重新做 新做 | 更新日期: 2023-09-27 17:53:14
我只是c#的初学者,我在一个控制台应用程序上工作,我希望程序能够读取用户的输入并执行订单,直到他有0 HP。基本上就是"格斗游戏"
我的问题现在,我只能请求用户输入一次(它只会执行程序曾经得到的输入)。我想添加额外的战斗选项,以及,但现在我被困在这里。
static void Main(string[] args)
{
int playerHealth = 100;
int playerDamageSword = 10;
int playerDamageFire = 10;
int playerHeal = 20;
int enemyHealth = 100;
Console.WriteLine("Hey! Enter your Character Name!");
string name = Console.ReadLine();
Console.Clear();
Console.WriteLine("Okay! " + name + " You have encountered an Enemy! Which Action do you choose?'n1.Sword-Attack'n2.Fire-Attack'n3.Heal'n8.Flee'n9.Quit'n" );
string Input = Console.ReadLine();
do
{
if (Input == "1")
{
Console.WriteLine("You hit your Opponment for 10 Damage!");
enemyHealth = enemyHealth - 10;
Console.WriteLine("Your Opponment hit you back for 10 Damage!");
playerHealth = playerHealth - 10;
Console.WriteLine("Your Current Hp: " + playerHealth);
Console.WriteLine("Opponment Current HP: " + enemyHealth);
}
else if (Input == "2")
{
Console.WriteLine("You healed yourself for 20 HP");
playerHealth = playerHealth + 20;
Console.WriteLine("Your Current Hp: " + playerHealth);
Console.WriteLine("Opponment Current HP: " + enemyHealth);
}
} while (playerHealth > 0);
你已经很接近了,你需要将input read移到循环中,像这样:
string Input;
do
{
Input = Console.ReadLine();
if (Input == "1")
{
Console.WriteLine("You hit your Opponment for 10 Damage!");
enemyHealth = enemyHealth - 10;
Console.WriteLine("Your Opponment hit you back for 10 Damage!");
playerHealth = playerHealth - 10;
Console.WriteLine("Your Current Hp: " + playerHealth);
Console.WriteLine("Opponment Current HP: " + enemyHealth);
}
else if (Input == "2")
{
Console.WriteLine("You healed yourself for 20 HP");
playerHealth = playerHealth + 20;
Console.WriteLine("Your Current Hp: " + playerHealth);
Console.WriteLine("Opponment Current HP: " + enemyHealth);
}
} while (playerHealth > 0);
这将做什么:
- 开始循环
- 获取用户输入并将其存储到
Input
字符串 - 检查
if (Input == "1")
或else if (Input == "2")
状态 - 检查
(playerHealth > 0)
,然后移动到循环的顶部或退出循环