我理解用于确定某些内容是否为闰年的代码行,并且我已经看到了用于确定数字总和的代码行但我不知道如何组合他们俩。当我运行它时,我得到一个错误说
In function "main":
error. "else" without a previous "if"
error. "else" without a previous "if"
代码:
#include <stdio.h>
int main()
{
int year,sum=0,r;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
for(;year!=0;year=year/10){
r=year%10;
sum=sum+r;
}
printf("%d is a leap year and the sum is %d.\n", year,sum);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
for(;year!=0;year=year/10){
r=year%10;
sum=sum+r;
}
printf("%d is a leap year and the sum is %d.\n", year,sum);
else
printf("%d is not a leap year.\n", year);
return 0;
}
答案 0 :(得分:1)
在您的代码中,对于某些{
语句,您不使用}
if
对的块语法。您后来插入了一些额外的printf
语句,打破了语法。
鉴于您的技能水平,始终对所有复合语句使用块语法是个好主意。恕我直言,即使是高级程序员也不应该使用单一语句语法来处理除最简单的复合语句之外的任何内容,如果有的话。
year
的输出不正确,因为您修改了for
循环中的变量。使用另一个变量进行此计算。
以下是修改后的代码:
#include <stdio.h>
int main(void) {
int year, sum = 0, y, r;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if (year % 400 == 0) {
for (y = year; y != 0; y = y / 10) {
r = y % 10;
sum = sum + r;
}
printf("%d is a leap year and the sum is %d.\n", year, sum);
} else
if (year % 100 == 0) {
printf("%d is not a leap year.\n", year);
} else
if (year % 4 == 0) {
for (y = year; y != 0; y = y / 10) {
r = year % 10;
sum = sum + r;
}
printf("%d is a leap year and the sum is %d.\n", year, sum);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
请注意,可以使用闰年的复合测试简化代码(根据公历):
#include <stdio.h>
int main(void) {
int year, sum, y, r;
printf("Enter a year to check if it is a leap year\n");
if (scanf("%d", &year) == 1) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
for (sum = 0, y = year; y != 0; y = y / 10) {
r = y % 10;
sum = sum + r;
}
printf("%d is a leap year and the sum is %d.\n", year, sum);
} else {
printf("%d is not a leap year.\n", year);
}
} else {
printf("input error.\n");
}
return 0;
}
答案 1 :(得分:0)
代码中的一些问题 -
问题没有。 1 -
“if”程序中的语句包含多个语句,但未将它们括在{}。
问题没有。 2 -
您正在修改“年”变量的值,并将该年份作为闰年打印出来。因此,year的值将始终打印为0。 备份变量“year”值,然后按步骤计算总和,并在printf中打印备份值。
#include <stdio.h>
int main()
{
int year,sum=0,r;
int year_val;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
year_val = year;
if ( year%400 == 0) {
for(;year!=0;year=year/10){
r=year%10;
sum=sum+r;
}
printf("%d is a leap year and the sum is %d.\n", year_val,sum);
} else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 ) {
for(;year!=0;year=year/10){
r=year%10;
sum=sum+r;
}
printf("%d is a leap year and the sum is %d.\n", year_val,sum);
} else
printf("%d is not a leap year.\n", year);
return 0;
}
答案 2 :(得分:0)
您的代码中存在大量语法错误。首先尝试编写干净的代码。
以下是纠正
所需的错误 a)删除第5行的额外分号
b)在第8,18行的if语句中添加大括号{}
c)添加一个临时变量来保存年份,这样如果年份是闰年,则
你可以在那里打印temp。因为年份变量将减少到0。
我刚刚纠正了上面提到的事情,现在你的代码就像魅力一样!
这是修改后的代码。详细了解大括号{}
#include <stdio.h>
int main()
{
int year,sum=0,r;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
int temp = year;
if ( year%400 == 0){
for(;year!=0;year=year/10){
r=year%10;
sum=sum+r;
}
printf("%d is a leap year and the sum is %d.\n", temp,sum);
}
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 ){
for(;year!=0;year=year/10){
r=year%10;
sum=sum+r;
}
printf("%d is a leap year and the sum is %d.\n", temp,sum);
}
else
printf("%d is not a leap year.\n", year);
return 0;
}