2013-10-18 307 views
1

我在介紹編程並且努力完成這項任務,任何幫助都將被讚賞!下面是代碼:期望的聲明說明符錯誤

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

int main(void) 
{ 

int cardNum(int firstCard, int secondCard; 
int highLow; 
int score; 

score = 0; 
srand(time(NULL)); 

printf("The current card is a %d\n" ,firstCard(2,14)); 
printf("\n Will the next card be higher(1) or lower(2)? (press 0 to quit)"); 
scanf("%d" ,highLow); 

if (cardNum > 1 && cardNum < 11) 
{   
    printf ("The card is: %d ,secondCard."); 
}   
else if (cardNum == 11) 
{ 
    printf ("The card is: Jack"); 
}  
else if (cardNum == 12) 
{ 
    printf ("The card is: Queen"); 
}  
else if (cardNum == 13) 
{ 
    printf ("The card is: King"); 
}  
else if cardNum == 14) 
{ 
    printf ("The card is: Ace"); 
}  
{ 
if (highLow == 1 && secondCard > firstCard) || (highLow == 2, && secondCard < firstCard) 
    { 
     score = score + 1; 
     printf ("\n You have guessed correctly."); 
     printf ("\n Your current score is %d ,score!\n"); 
     printf("The current card is a ("%d" ,cardOne). \n Will the next card be higher(1) or lower(2)? (press 0 to quit)"); 
    }  
else if (highLow == 1, && secondCard < firstCard) || (highLow == 2, && secondCard > firstCard) 
    { 
     score = score - 1; 
     printf ("The card is: %d ,secondCard."); 
     printf ("\n You have guessed incorrectly."); 
     printf ("\n Your current score is %d ,score!\n"); 
     printf ("The current card is a %d ,cardOne."); 
     printf ("\n Will the next card be higher(1) or lower(2)? (press 0 to quit)"); 
    } 
else if (secondCard == firstCard) 
    { 
     printf ("\n Matching cards, no change in score"); 
    } 
else if (highLow == 0) 
    { 
     printf ("\n Thanks for playing! Your final score is %d, score."); 
    } 
else 
    { 
     printf ("\n Incorrect input. Please enter 0, 1 or 2");  
    } 
}  
return(0); 
} 

這些錯誤:

a3.c: In function ‘main’: 
a3.c:24:5: error: expected declaration specifiers or ‘...’ before ‘score’ 
a3.c:25:5: error: expected declaration specifiers or ‘...’ before ‘srand’ 
a3.c:27:5: error: expected declaration specifiers or ‘...’ before ‘printf’ 
a3.c:28:5: error: expected declaration specifiers or ‘...’ before ‘printf’ 
a3.c:29:5: error: expected declaration specifiers or ‘...’ before ‘scanf’ 
a3.c:31:5: error: expected declaration specifiers or ‘...’ before ‘if’ 
a3.c:52:9: error: ‘highLow’ undeclared (first use in this function) 
a3.c:52:9: note: each undeclared identifier is reported only once for each function it appears in 
a3.c:52:25: error: ‘secondCard’ undeclared (first use in this function) 
a3.c:52:38: error: ‘firstCard’ undeclared (first use in this function) 
a3.c:52:49: error: expected expression before ‘||’ token 

我知道未申報的部分原因是我應該做的隨機數,並將其儲存,但我仍然還沒有很明白這一點(任何建議都會很棒),但主要是我擔心預期聲明說明符的廣泛列表。任何幫助,將不勝感激!

+1

看這句話:'INT cardnum中(INT firstCard,INT secondCard; ' – hek2mgl

+0

和這一行:'if(highLow == 1 && secondCard> firstCard)|| (highLow == 2,&& secondCard Pankrates

+0

和這一行:'else if(highLow == 1,&& secondCard firstCard)' – Pankrates

回答

0

看這句話:

int cardNum(int firstCard, int secondCard; 

我想這應該是:

int cardNum, int firstCard, int secondCard; 

同樣的if語句是錯誤的。我想這應該是:

if ((highLow == 1 && secondCard > firstCard) || (highLow == 2, && secondCard < firstCard)) 

else if ((highLow == 1 && secondCard < firstCard) || (highLow == 2 && secondCard > firstCard)) 

你應該從頭開始學習C。

1
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

int main(void) 
{ 

int cardNum, int firstCard, int secondCard; 
int highLow; 
int score; 

score = 0; 
srand(time(NULL)); 

printf("The current card is a %d\n", firstCard(2,14)); 
printf("\n Will the next card be higher(1) or lower(2)? (press 0 to quit)"); 
scanf("%d" ,highLow); 

if (cardNum > 1 && cardNum < 11) 
{   
    printf ("The card is: %d ,secondCard."); 
}   
else if (cardNum == 11) 
{ 
    printf ("The card is: Jack"); 
}  
else if (cardNum == 12) 
{ 
    printf ("The card is: Queen"); 
}  
else if (cardNum == 13) 
{ 
    printf ("The card is: King"); 
}  
else if cardNum == 14) 
{ 
    printf ("The card is: Ace"); 
}  
{ 
if ((highLow == 1 && secondCard > firstCard) || (highLow == 2 && secondCard < firstCard)) 
    { 
     score = score + 1; 
     printf ("\n You have guessed correctly."); 
     printf ("\n Your current score is %d ,score!\n"); 
     printf("The current card is a ("%d" ,cardOne). \n Will the next card be higher(1) or lower(2)? (press 0 to quit)"); 
    }  
else if ((highLow == 1 && secondCard < firstCard) || (highLow == 2 && secondCard > firstCard)) 
    { 
     score = score - 1; 
     printf ("The card is: %d ,secondCard."); 
     printf ("\n You have guessed incorrectly."); 
     printf ("\n Your current score is %d ,score!\n"); 
     printf ("The current card is a %d ,cardOne."); 
     printf ("\n Will the next card be higher(1) or lower(2)? (press 0 to quit)"); 
    } 
else if (secondCard == firstCard) 
    { 
     printf ("\n Matching cards, no change in score"); 
    } 
else if (highLow == 0) 
    { 
     printf ("\n Thanks for playing! Your final score is %d, score."); 
    } 
else 
    { 
     printf ("\n Incorrect input. Please enter 0, 1 or 2");  
    } 
}  
return(0); 
} 
1

1)int cardNum(int firstCard, int secondCard;

取而代之的是,你應該寫

int cardNum, int firstCard, int secondCard;

,因爲你是在聲明變量

2)的if (highLow == 1 && secondCard > firstCard) || (highLow == 2, && secondCard < firstCard)

而非低於

給這個寫聲明

if (highLow == 1 && secondCard > firstCard) || (highLow == 2 && secondCard < firstCard)

3)else if (highLow == 1, && secondCard < firstCard) || (highLow == 2, && secondCard > firstCard)

你應該寫

else if (highLow == 1 && secondCard < firstCard) || (highLow == 2 && secondCard > firstCard)