鱼C论坛

 找回密码
 立即注册
查看: 977|回复: 4

[已解决]switch报错

[复制链接]
发表于 2020-1-4 14:54:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面这段代码计算某一天是一年中的第几天。switch的第七个case开始报错。删掉case 7 ~ case 12则可以运行。
报错:||=== Build: Debug in project (compiler: GNU GCC Compiler) ===|
D:\myCODE\c\project\main.c||In function 'Days':|
D:\myCODE\c\project\main.c|19|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|19|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|20|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|20|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|21|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|21|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|22|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|22|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|23|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|23|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|24|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|24|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c||In function 'main':|
D:\myCODE\c\project\main.c|42|error: expected expression before '=' token|
||=== Build failed: 13 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>

int Days(int *a, int *b);
int Days(int *a, int *b){
    unsigned int x = *a;
    int y = *b;
    int days;
    switch(x){
        case 1: days = y;break;
        case 2: days = 31+y;break;
        case 3: days = 31+28+y;break;
        case 4: days = 31+28+31+y;break;
        case 5: days = 31+28+31+30+y;break;
        case 6: days = 31+28+31+30+31+y;break;
        case 7: days = 31+28+31+30++31+30+y;break; //从这里开始会报错
        case 8: days = 31+28+31+30++31+30+31+y;break;
        case 9: days = 31+28+31+30++31+30+31+31+y;break;
        case 10: days = 31+28+31+30++31+30+31+31+30+y;break;
        case 11: days = 31+28+31+30++31+30+31+31+30+31+y;break;
        case 12: days = 31+28+31+30++31+30+31+31+30+31+30+y;break;
        default: break;
    }
    return days;
}

int main(void)
{
    int year, month, day;
    int DAY;
    printf("please enter year-month-day.eg: 1995-5-3");
    scanf("%d-%d-%d",&year, &month, &day);
    if(year%4 ==0 || year%400 == 0){
        DAY = Days(&month, &day) + 1;
        printf("this is the %d th day of %d", DAY, year);
    }
    else
        printf("this is the %d th day of %d", Days(&month, &day), year);
    return = 0;
}

最佳答案
2020-1-4 15:05:57
zhuwei9999 发表于 2020-1-4 15:03
谢谢,你这样是正确运行的。请问改动了哪里?


        case 7: days = 31+28+31+30++31+30+y;break;
        case 8: days = 31+28+31+30++31+30+31+y;break;
        case 9: days = 31+28+31+30++31+30+31+31+y;break;
        case 10: days = 31+28+31+30++31+30+31+31+30+y;break;
        case 11: days = 31+28+31+30++31+30+31+31+30+31+y;break;
        case 12: days = 31+28+31+30++31+30+31+31+30+31+30+y;break;

还有程序最后:

return = 0;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-1-4 14:57:42 | 显示全部楼层
代码有问题,试试这样:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>

int Days(int *a, int *b);
int Days(int *a, int *b){
    unsigned int x = *a;
    int y = *b;
    int days;
    switch(x){
        case 1: days = y;break;
        case 2: days = 31+y;break;
        case 3: days = 31+28+y;break;
        case 4: days = 31+28+31+y;break;
        case 5: days = 31+28+31+30+y;break;
        case 6: days = 31+28+31+30+31+y;break;
        case 7: days = 31+28+31+30+31+30+y;break;
        case 8: days = 31+28+31+30+31+30+31+y;break;
        case 9: days = 31+28+31+30+31+30+31+31+y;break;
        case 10: days = 31+28+31+30+31+30+31+31+30+y;break;
        case 11: days = 31+28+31+30+31+30+31+31+30+31+y;break;
        case 12: days = 31+28+31+30+31+30+31+31+30+31+30+y;break;
        default: break;
    }
    return days;
}

int main(void)
{
    int year, month, day;
    int DAY;
    printf("please enter year-month-day.eg: 1995-5-3");
    scanf("%d-%d-%d",&year, &month, &day);
    if(year%4 ==0 || year%400 == 0){
        DAY = Days(&month, &day) + 1;
        printf("this is the %d th day of %d", DAY, year);
    }
    else
        printf("this is the %d th day of %d", Days(&month, &day), year);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-1-4 15:03:36 | 显示全部楼层
zltzlt 发表于 2020-1-4 14:57
代码有问题,试试这样:

谢谢,你这样是正确运行的。请问改动了哪里?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-4 15:05:57 | 显示全部楼层    本楼为最佳答案   
zhuwei9999 发表于 2020-1-4 15:03
谢谢,你这样是正确运行的。请问改动了哪里?


        case 7: days = 31+28+31+30++31+30+y;break;
        case 8: days = 31+28+31+30++31+30+31+y;break;
        case 9: days = 31+28+31+30++31+30+31+31+y;break;
        case 10: days = 31+28+31+30++31+30+31+31+30+y;break;
        case 11: days = 31+28+31+30++31+30+31+31+30+31+y;break;
        case 12: days = 31+28+31+30++31+30+31+31+30+31+30+y;break;

还有程序最后:

return = 0;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-1-4 15:07:04 | 显示全部楼层
zltzlt 发表于 2020-1-4 15:05
case 7: days = 31+28+31+30++31+30+y;break;
        case 8: days = 31+28+31+30++31+30+31 ...

谢谢,我太粗心了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 05:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表