简体   繁体   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;
}

I try to make a loop if the num<=0 then the program will back to at begin, but it doesn't allow me to press any bottoms.如果 num<=0,我尝试创建一个循环,然后程序将回到开始,但它不允许我按下任何底部。

With all respect, all your code is wrong!恕我直言,您的所有代码都是错误的!

First of all, don't put "%d\n" just use "%d" instead.首先,不要把 "%d\n" 用 "%d" 代替。 On the same line, try to use "scanf".在同一行,尝试使用“scanf”。

Then by reading your code, what you were trying to do (I assume) is to enter a loop until the input was greater than 0, the problem is that you don't do it that way.然后通过阅读你的代码,你试图做的(我假设)是进入一个循环,直到输入大于 0,问题是你不这样做。 To do so, you need to use do/while instead.为此,您需要改用 do/while。

Then your second while doesn't have any curly braces.然后你的第二个 while 没有任何花括号。 So, I did your code work just for fun:)所以,我做你的代码只是为了好玩:)

#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;
}

Hope it helps.希望能帮助到你。

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

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