gpt4 book ai didi

c# - 试图修复循环

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:32 24 4
gpt4 key购买 nike

我一直在为我的高级RAP项目开发游戏,直到最近,我才开始进行此循环。当球员或敌人的生命值等于0时,我需要一种方法来迭代此值。我不希望它们都等于0,而这两个示例都这样做。我尝试过几种重写方式,但是没有用。感谢所有对此提供帮助的人。

while (!(enemyHealth == 0 || playerHealth == 0)
{ //START WHILE
Console.WriteLine("[System:] Here are the list of moves that you can do:\n1)Punch\n2)Kick\n3)Round House Kick");
Console.WriteLine();
decision = Console.ReadLine();
Console.WriteLine();
decisionNumber = int.Parse(decision);
if (decisionNumber < 1 || decisionNumber > 3)
{ //STARTS IF
throw new Exception();
} //ENDS IF
else
{ //STARTS ELSE
enemyHealth = enemyHealth - attackNumber;
playerHealth = playerHealth - enemyAttackNumber;
Console.WriteLine();
Console.WriteLine("[System:] Enemy Health: " + enemyHealth + "\nYour Health: " + playerHealth);
} //ENDS ELSE
} //ENDS WHILE



while (enemyHealth > 0 || playerHealth > 0)
{ //START WHILE
Console.WriteLine("[System:] Here are the list of moves that you can do:\n1)Punch\n2)Kick\n3)Round House Kick");
Console.WriteLine();
decision = Console.ReadLine();
Console.WriteLine();
decisionNumber = int.Parse(decision);
if (decisionNumber < 1 || decisionNumber > 3)
{ //STARTS IF
throw new Exception();
} //ENDS IF
else
{ //STARTS ELSE
enemyHealth = enemyHealth - attackNumber;
playerHealth = playerHealth - enemyAttackNumber;
Console.WriteLine();
Console.WriteLine("[System:] Enemy Health: " + enemyHealth + "\nYour Health: " + playerHealth);
} //ENDS ELSE
} //ENDS WHILE


任何人都可以解决这个问题,因为我遇到了重大问题。

最佳答案

可以简化条件,因为负健康级别为!= 0会产生逻辑错误,我们还需要保持循环运行,直到enemyplayer的健康级别为> 0

while ((enemyHealth > 0 && playerHealth > 0))




好像是,问题在于您在循环中进行编码,

throw new Exception();似乎是一个错误的逻辑,我的建议是放弃忽略错误的输入。

可以像

while ((enemyHealth > 0 || playerHealth > 0))
{
Console.WriteLine("[System:] Here are the list of moves that you can do:\n1)Punch\n2)Kick\n3)Round House Kick");
Console.WriteLine();
decision = Console.ReadLine();
Console.WriteLine();
decisionNumber = int.Parse(decision);
if (decisionNumber >= 1 && decisionNumber <= 3)
{

enemyHealth = enemyHealth - attackNumber;
playerHealth = playerHealth - enemyAttackNumber;
Console.WriteLine();
Console.WriteLine("[System:] Enemy Health: " + enemyHealth + "\nYour Health: " + playerHealth);
}
else
{
Console.WriteLine("Invalid Input : press enter to input again");
Console.ReadKey();
}

}

关于c# - 试图修复循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030989/

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