Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
我在行上说这个问题,如果它说(knightATKC == 2 && knightATKC == 4)我说永远不会执行代码。并且无法正常工作。如果有人知道如何解决,那就太好了。不用担心您可能会看到的未使用的变量,我会在稍后使用它,但是现在我不知道如何修复代码将永远不会执行。对不起,我英语不好:/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>

int main()
{
char enter = 0;
char pNAME[30];
char pGENDER;
int pAGE;
int pATK;
int pHP = 20;
int pATKC;
int knightATK = 3;
int knightHP = 15;
int knightATKC = 0;
int kingATK = 5;
int kingHP = 30;
int kingATKC;

printf("While you are enjoying your breakfast at your house, suddenly you get teleported to a different dimension and no one to be seen except an old man.\n");
printf("\nOld Man: What is your name warrior?\n");
printf("\nInsert name:\n");
scanf("%s",pNAME);
fpurge( stdin );

printf("\nInsert gender (m/f):\n");
scanf("%c",&pGENDER);
fpurge( stdin );
while ( pGENDER != 'f' &&pGENDER != 'm' )
{
    printf("\nInvalid entry, please try again.\n");
    printf("\nInsert gender (m/f):\n");
    scanf("%c",&pGENDER);
}

printf("\nOld Man: Now how old are you young warrior?\n");
printf("\nInsert Age:\n");
scanf("%d",&pAGE);
fpurge( stdin );
if ( pAGE < 18)
{
    printf("\nOy mate! No stalker!\n");
    printf("\nTeleporting back to reality...\n");

    return 0;
}

if ( pAGE > 80)
{
    printf("\nNo oldies allowed!\n");
    printf("\nTeleporting back to reality...\n");

    return 0;
}

printf("\nOld Man: Welcome warrior I fear the knights has taken over our kingdom, and you, %s, are the only one who can save us.\n",pNAME);
printf("\n%s: How am I suppose to do that?[Enter]\n",pNAME);
if (enter != '\r' && enter != '\n') { enter = getchar(); }
printf("HI");

while (pHP != 0 && knightHP != 0)
{
    pATK = 5;
    knightATK = 3;

    knightATKC = ( rand()%5);

    if (knightATKC == 2 && knightATKC == 4)
    {
        printf("The knight swings his sword at %s",pNAME);
        printf("T\nhe knight hits %s for 3HP\n",pNAME);
        pHP = pHP - 3;
    }
    else if (knightATKC == 3)
    {
        printf("\nThe knight slashes his sword at %s\n",pNAME);
        printf("\nThe knight critically hit %s for 5 HP\n",pNAME);
        pHP = pHP - 5;
    }
    else
    {
        printf("\nThe knight swings his sword at %s\n",pNAME);
        printf("\nThe knight fails to hit %s\n",pNAME);
        printf("\nPress [Enter] to attack\n");
    }

}}

最佳答案

您的问题不言自明。 knightATKC == 2 && knightATKC == 4表示knightATKC同时为2和4,这是不可能的。也许您是说||(布尔值)而不是&&(布尔值和)?

关于c - 代码将永远不会被执行C,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32292420/

10-10 14:03