使用“ If语句”;在基本的C编程中。如何正确格式化它们? [英] Using "If Statements" in basic C programming. How can I properly format them?
问题描述
首先,下面是我到目前为止所掌握的代码。
To begin, the code that I have got up to this point is below.
计算器可以完成我想要做的所有事情,但实际上它仍然包含计算中为负数。如果半径表示的数字小于零,我不希望它做任何事情,但是如果数字为非负数,我仍然希望计算。但是,我在使用if语句时遇到问题。我以前从未使用过这些,因为我真的只是一个初学者。我只需要向正确的方向推进即可。我是否需要其他?
The calculator does everything I want it to, minus the fact that it still includes negative numbers in its calculations. I would like for it to do nothing if the number presented as the radius is less than zero, yet still calculate if the number is non-negative. However, I am having issues with using an if statement. I have not used these before, as I am really just a beginner. I just need a push in the right direction. Do I need to have an "else"? Any help is greatly appreciated!
#include <stdio.h>
int radius, area;
main()
{
printf("Enter radius: ");
scanf("%d", &radius);
{
if (&radius > -1); {
area = (int)(3.14159 * radius * radius);
printf("Area = %d\n", area);}
else {
return(0); }
}
return(0);
}
推荐答案
删除半冒号
删除此行的分号
if (&radius > -1); {
应该是
if (radius > -1) {
应该这样做以便更轻松地跟踪if-else语句
更改这些行
printf("Area = %d\n", area);}
return(0); }
至
printf("Area = %d\n", area);
}
return(0);
}
这里是if-else语句的样式,我认为这更容易
if (condition) {
statements;
}
else if (condition) {
statements;
}
else {
statements;
}
这篇关于使用“ If语句”;在基本的C编程中。如何正确格式化它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!