预期的';)';在';之前';代币C
我正在尝试制作一个程序,用于打印0到100之间所有偶数的^2、^4和^(1/3)。这就是我所拥有的预期的';)';在';之前';代币C,c,loops,variables,C,Loops,Variables,我正在尝试制作一个程序,用于打印0到100之间所有偶数的^2、^4和^(1/3)。这就是我所拥有的 #include <stdio.h> #include <math.h> main(){ int a, b, c, i; for (i=0; i<100; i=i+2;) a = (1*1); b = (i*i*i*i); c = pow(i, (1/3)); printf("%d, %d, %d", a,
#include <stdio.h>
#include <math.h>
main(){
int a, b, c, i;
for (i=0; i<100; i=i+2;)
a = (1*1);
b = (i*i*i*i);
c = pow(i, (1/3));
printf("%d, %d, %d", a, b, c);
return 0;
}
这是我在c的第一天,所以我现在真的很困惑 for循环应该是这样的:
for (initialization; condition; increment/decrement)
for (i = 0; i < 100; i = i + 2)
你的不是那样的。请注意,括号内正好有两个分号。删除
i=i+2之后for
语句中的分号代码>
for (i=0; i<100; i=i+2)
for
语句的括号中的三个部分是可选的,因此您可以使用类似以下内容的内容,其结构类似于while
循环
i=0;
for ( ; i < 10; ) {
printf (" i = %d\n", i);
i=i+2;
}
上述情况类似于:
int i, j, k;
j = 0;
i = j;
k = 3;
while (i < 10 && k > 1) {
printf (" i = %d j = %d k = %d\n", i, j, k);
i++;
j+=2;
}
inti,j,k;
j=0;
i=j;
k=3;
而(i<10&&k>1){
printf(“i=%dj=%dk=%d\n”,i,j,k);
i++;
j+=2;
}
您将for循环的初始值设定项和测试条件混合在一起。将其更改为如下所示:
for (i=0; i<100; i=i+2)
for(i=0;ifor循环语句只需要两个分号来分隔它的三个部分
所以它应该看起来像:
for (initialization; condition; increment/decrement)
for (i = 0; i < 100; i = i + 2)
这不起作用。在C中,1/3
的计算结果为0,因为C编译器将其视为整数除法。您需要使用1.0/3.0
您的for循环不正确
for (i=0, i<100; i=i+2;)
for(i=0,i正如@Bill Lynch所说,for循环需要:
#include <stdio.h>
#include <math.h>
main(){
int a, b, c, i;
for (i=0; i<100; i=i+2) // changed comma to a semicolon
{//add this bracket
a = (1*1);
b = (i*i*i*i);
c = pow(i, (1/3));
printf("%d, %d, %d", a, b, c);
}//add this bracket
return 0;
}
#包括
#包括
main(){
INTA、b、c、i;
对于(i=0;i,以下代码显示了对发布代码的所有建议更正
插入注释以解释更改
#include <stdio.h> // printf
#include <math.h> // pow
int main() // <-- use valid return type
{
int a; // value ^2
int b; // value ^4
double c; // value ^1/3 note: pow() returns a double
int i; // loop index
// the following loop calculates all the request values
// from 0 through 98. did you want to include '100'?
for (i=0; i<100; i+=2) // < corrected for statement
{ // < added braces so whole code block loops
a = (i*i); // < squared value, not 1
b = (i*i*i*i);
c = pow( (double)i, (1.0/3.0) ); // < corrected integer divide
printf("%d, %d, %lf \n", a, b, c);
// properly printed double,
// added newline
// so output displayed immediately
} // end for
return 0;
} // end function: main
#包括//printf
#包括//pow
int main()//这里甚至没有问题,您的编译器已经告诉您错误。在C中,要在代码块(如“for”循环)中执行多行代码,代码块需要包含一个左大括号和一个右大括号;“{”和“}”类似于:for(…){…}否则,只有下一条语句被视为循环的一部分。行:“a=(1*1);”将始终导致“a”被设置为1。我怀疑实际需要的是“I”的平方,而不是1的平方。建议:“a=I*I)顺便说一句,这个for循环将只包含下一个语句。如果您想包含更多语句,那么您应该对属于for
循环的所有语句使用大括号。哎呀,最后没有捕捉到那个语句
for (i=0, i<100; i=i+2;)
for (i=0; i<100; i=i+2)
#include <stdio.h>
#include <math.h>
main(){
int a, b, c, i;
for (i=0; i<100; i=i+2) // changed comma to a semicolon
{//add this bracket
a = (1*1);
b = (i*i*i*i);
c = pow(i, (1/3));
printf("%d, %d, %d", a, b, c);
}//add this bracket
return 0;
}
#include <stdio.h> // printf
#include <math.h> // pow
int main() // <-- use valid return type
{
int a; // value ^2
int b; // value ^4
double c; // value ^1/3 note: pow() returns a double
int i; // loop index
// the following loop calculates all the request values
// from 0 through 98. did you want to include '100'?
for (i=0; i<100; i+=2) // < corrected for statement
{ // < added braces so whole code block loops
a = (i*i); // < squared value, not 1
b = (i*i*i*i);
c = pow( (double)i, (1.0/3.0) ); // < corrected integer divide
printf("%d, %d, %lf \n", a, b, c);
// properly printed double,
// added newline
// so output displayed immediately
} // end for
return 0;
} // end function: main