我的代码如下:
int main(){
int choice=(1,2,3);
printf("1- Finabocci sequence\n");
printf("2- Check valid date\n");
printf("3- Quit\n");
printf("Choose an operation: \n");
scanf("%d", &choice);
switch(choice){
case 1:
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while(nextTerm <= n)
{
printf("%d, ",nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
break;
}
case 2:
{
int validDate(int d,int m,int y){
if (m<1||m>12) return 0;
else if (m==1||m==3||m==5||m==7||m==8||m==10||m==12){
if (d>=1&&d<=31) return 1;
else return 0;
}
else if (m==4||m==6||m==9||m==11) {
if (d>=1&&d<=30) return 1;
else return 0;
}
else if (m==2) {
if (y%400==0||(y%4==0&&y%100!=0)) {
if (d>=1&&d<=29) return 1;
else return 0;
}
else if (d>=1&&d<=28) return 1;
else return 0;
}
}
int main(int validDate(int d,int m,int y)) {
int d,m,y;
printf ("Input day: ");
scanf("%d",&d);
printf ("Input month: ");
scanf("%d",&m);
printf ("Input year: ");
scanf("%d",&y);
if(validDate(d,m,y)!=0) printf("valid date");
else printf("invalid date");
return 0;
}
break;
}
default: printf("exit?");
getchar();
}
return 0;
}
我真的不知道option2根本没有运行,就像第一个和第三个一样。我该如何解决这个问题:D 例如:我单独运行Option2代码及其工作,但是当我放入交换机时,它将根本无法工作
答案 0 :(得分:2)
您将主要功能称为主要功能。我为您修复了代码。
int main(){
int choice=(1,2,3);
printf("1- Finabocci sequence\n");
printf("2- Check valid date\n");
printf("3- Quit\n");
printf("Choose an operation: \n");
scanf("%d", &choice);
switch(choice){
case 1:
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while(nextTerm <= n)
{
printf("%d, ",nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
break;
}
case 2:
{
int validDate(int d,int m,int y){
if (m<1||m>12) return 0;
else if (m==1||m==3||m==5||m==7||m==8||m==10||m==12){
if (d>=1&&d<=31) return 1;
else return 0;
}
else if (m==4||m==6||m==9||m==11) {
if (d>=1&&d<=30) return 1;
else return 0;
}
else if (m==2) {
if (y%400==0||(y%4==0&&y%100!=0)) {
if (d>=1&&d<=29) return 1;
else return 0;
}
else if (d>=1&&d<=28) return 1;
else return 0;
}
}
int d,m,y;
printf ("Input day: ");
scanf("%d",&d);
printf ("Input month: ");
scanf("%d",&m);
printf ("Input year: ");
scanf("%d",&y);
if(validDate(d,m,y)!=0) printf("valid date\n");
else printf("invalid date \n");
}
default: printf("exit?");
getchar();
}
return 0;
}
答案 1 :(得分:1)
第一件事是int main(int validDate(int d,int m,int y))
应该像这样int validDate(int d,int m,int y)
在您的情况下,这些代码不能像这样。
int choice=(1,2,3);
-错误
int choice= 0;
-对
另一件事是
nextTerm = t1 + t2;
输出-:0,1,1,2,3
->错误
nextTerm = t2 + 1;
输出-:0,1,2,3
->正确
另一件事是,在编译代码时,必须在每个break;
和{{1 }}您已给
最后的事情是遵循好的编码方法。它还可以帮助您识别错误
此处为更正的代码
cases