2016-05-12 36 views
2

繼續說else語句出現錯誤,並且應該在else之前出現錯誤。 我不能真正弄明白什麼是錯 如果有人可以幫助我走出這將是巨大修復它出現的代碼,在'else'之前說錯誤預期表達式

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


int random1_6() 
{ 
    return ((rand() % 6) + 1); 
} 

int main(void) 
{ 
    int a, b, c, d, equal, sum; 
    srand(time(NULL)); 
    a = random1_6(); 
    b=random1_6(); 
    sum = a + b; 
    printf("\n The player rolled: %d + %d = %d. \n the players point is %d. \n", a, b, sum, sum); 
    if (sum ==7); 
    { 
     printf("The player wins. \n"); 
    } 
    else (sum !=7); 
    { 
     c = random1_6(); 
     d=random1_6(); 
     equal = c + d;  
     printf("\n The player rolled: %d + %d = %d", c, d, equal); 
} 
+1

刪除''你else語句。 – claudios

+0

您還必須在if(sum == 7)之後移除逗號(;); – Carlo

回答

-1

你else語句是錯誤的。 這裏是if else syn文本。

if (some-condition) { 
} else{ 
} 

或者你可以使用

if (some-condition) { 
} else if (some-condition) { 
} 

只是刪除(總和!= 7)您的代碼將工作。

-1

刪除不必要的分號。

if (sum == 7) { 
printf("The player wins. \n"); 
} else { 
c = random1_6(); 
d = random1_6(); 
equal = c + d; 
printf("\n The player rolled: %d + %d = %d", c, d, equal); 
} 
0

(1)你不必把;if之後。如果你寫:

if(condition); { block; } 

Ç其解釋爲:檢查condition,不管是結果總是執行block

(2 + 3)else條件是錯誤的和無意義的:如果您達到else那麼sum != 7總是如此,所以不需要檢查它。

在任何情況下,如果你需要它,正確的語法是:

} 
else if(sum != 7) 
{ 

隨着if並沒有;

(4)你也忘了從main函數返回的東西,申報返回值爲int

(5)在您的代碼中,最後的}丟失了,也許只是錯誤的複製&粘貼?

這裏是你的代碼與上述更正:(!總和= 7)

int random1_6() 
{ 
    // enter code here 
    return ((rand() % 6) + 1); 
} 

int main(void) 
{ 
    int a, b, c, d, equal, sum; 

    srand(time(NULL)); 

    a = random1_6(); 
    b = random1_6(); 

    sum = a + b; 

    printf("\n The player rolled: %d + %d = %d. \n the players point is %d. \n", a, b, sum, sum); 

    if(sum == 7) // (1) Remove ";", it's wrong! 
    { 
     printf("The player wins. \n"); 
    } 
    else // (2) Remove "(sum !=7)", it's useless. (3) Remove ";" , it's wrong! 
    // else if(sum != 7) // This is the correct sintax for an else if, just in case :-) 
    { 
     c = random1_6(); 
     d = random1_6(); 
     equal = c + d;  
     printf("\n The player rolled: %d + %d = %d", c, d, equal); 
    } 

    return 0; // (4) The function is declared returning an int, so you must return something. 
} // (5) This bracket was missing :-) 

希望一切都清楚了

相關問題