即時學習c和即時通訊做一個excersice,我必須在1-99.99內打印一個10 x 4的數字表格,只打印數字大於那麼67然後打印每行的平均水平。但是我的代碼不會編譯和林不知道這裏爲什麼。是我「Stray''in program,'else'without'if'
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
int main(void)
{
int r;
int c;
double num;
double sum;
int count;
double avg;
for(c = 0; c < 10; c++)
{
avg = 0;
sum = 0;
count = 0;
for(r = 1; r< 5; r++)
{
num = (float) N * rand()/(RAND_MAX + 1.0);
if (num > 67.0)
count = count + 1;
sum = sum + num;
printf("%.2f, " num);
else
printf("%4s");
}
if(count = 0)
continue;
avg = (sum\count);
printf("%.2f |", avg);
printf("\n\n");
}
}
我得到了以下錯誤
line 24 expected '<' before num
line 25 else without a previous if statement
line 31 stray '\' in program
line 31 expected'>' before count
我看了看他們,但我不能弄明白嗎?
大括號都需要別的嗎?那麼如果在最底部,我也支持這個呢? –
@ZachSantiago如果'if' /'else' /'for'/'while' /等,則不需要大括號。代碼塊只是一個語句。然而,許多人認爲在這種情況下不使用它們是不好的做法,因爲它會導致混淆,並且如果稍後將特定代碼塊擴展爲多個語句會增加困難(在這種情況下,需要花括號來考慮內部語句給定的代碼塊)。 – ajp15243
@ZachSantiago只有當你有多個表述時才需要大括號,但即使你有一個表述,保留它們也是一種很好的做法。 – rullof