c# - 试图修复循环

标签 c# loops while-loop project

我一直在为我的高级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/

相关文章:

C# 8 异步流与 REST/RPC

c# - OpenGL ES 2.0/MonoTouch : Texture is colorized red

java - 将用户输入的数字转换为单词

loops - Netlogo while 循环仅一次

c - 程序不读取循环中的最后一个字符

c# - 在后台进行 UI 工作

c# - 使用 double.Parse 如果 Null 值,则值替换为 0

javascript - 如何使 for 循环与 async.parallel() 一起工作

php - 将中点添加到数组中并在下一次迭代中使用它们

c++ - 我的循环不会循环