繁体   English   中英

为什么这个 C 不再工作了?

[英]Why is this C while didn't working anymore?

#include<stdio.h>

int main(void)
{
    int num;
    int week;
    int days;
    printf("enter a day\n");
    scanf_s("%d\n", &num);
    
    
    while (num <= 0)
    {
        printf("your input is wrong, try again");
        num++;
       while (num > 0)
    
        week = num / 7;
        days = week * 7 - num;
        printf("%d days are %d week and %d days\n", &num, &week, &days);

    }
    
    return 0;
}

如果 num<=0,我尝试创建一个循环,然后程序将回到开始,但它不允许我按下任何底部。

恕我直言,您的所有代码都是错误的!

首先,不要把 "%d\n" 用 "%d" 代替。 在同一行,尝试使用“scanf”。

然后通过阅读你的代码,你试图做的(我假设)是进入一个循环,直到输入大于 0,问题是你不这样做。 为此,您需要改用 do/while。

然后你的第二个 while 没有任何花括号。 所以,我做你的代码只是为了好玩:)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int num = 0, week = 0, days = 0;

    do
    {
        printf("Enter a day: ");
        if(!scanf("%d", &num))
            exit(EXIT_FAILURE); // It at the time of scanning a number we fail to do so, we will exit the program execution
        if(num <= 0)
            puts("Your input is wrong! Try again.");
    } while (num <= 0);

    week = num / 7;
    days = num - (week * 7);

    printf("%d days are: %d week and %d days.\n", num, week, days);

    return 0;
}

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM