2015-08-30 165 views
-3

我有這個問題,它說,如果(knightATKC == 2 & & knightATKC == 4)我說,代碼將永遠不會執行。它將無法正常工作。如果有人知道如何解決這個問題,那會很好。不要擔心你可能會看到的未使用的變量,我將盡快使用它,但現在我不知道如何修復代碼將永遠不會執行。對不起,我可憐的英語:/代碼將永遠不會執行C

#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"); 
    } 

}} 
+2

如果你想要執行代碼,不要求一個永遠不會成立的條件! –

+0

@DiegoBasch有時開發人員會犯簡單的編碼錯誤。有時候會有混亂。有時候我們會給這個人帶來懷疑的好處。 – zaph

回答

3

你的問題是不言自明的。 knightATKC == 2 && knightATKC == 4表示knightATKC同時是2和4,這是不可能的。也許你的意思是||(布爾或)而不是&&(布爾和)?

3

壞聲明:

if (knightATKC == 2 && knightATKC == 4) 

這整個如果因爲&&運營商「和」聲明絕不是真實的,因此第一比較和第二都必須是真實的。但是,如果knightATKC等於2那麼knightATKC不能等於4

也許你想要的是 「或」

if (knightATKC == 2 || knightATKC == 4) 

如果以上任何一種情況。