繁体   English   中英

C程序没有给出正确的输出

[英]C program not giving correct output

我有以下代码,要求用户输入正数天,然后要求用户输入该天的最高和最低温度,但是在我运行该代码后,我程序会要求输入天数(s)它要求最高温度,然后仅在不要求最低温度和条件的情况下结束程序。 这是代码。

#include <stdio.h>
int main(void) {

    int days,condt;
    double high,low,aver;

    printf("Weather Analyzer \n");
    printf("================ \n");

    printf("Please enter a positive number of days:");
    scanf("%d", &days);



    if (days <= 0) {

            while (days <= 0) {
                    printf("Please enter a positive number of days:");
                    scanf("%d", &days);
            }
    }



    printf("Enter today's high:");
    scanf("%.2f", &high);

    printf("Enter today's low:");
    scanf("%.2f", &low);

    printf("Enter today's condition: (s: sunny, c: cloudy, p: precipitation");
    scanf("%d", &condt);

    aver = high + low / 2.0;

    printf("Today's average temperature is: %.2f", aver);

}

这是确切的输出:

Weather Analyzer
================
Please enter a positive number of days:3
Enter today's high:1
Enter today's low:Enter today's condition: (s: sunny, c: cloudy, p: precipitationToday's average temperature is: -0.00admin@machine:~/cprogram/weather>

这里highlowdouble scanf "%.2f"更改为"%lf" 为了获得适当的功能, condt声明为char。 然后,您可以通过在scanf使用" %c"来获取“ s”或“ c”值。

如果选择将condt用作int则使用1或2来指定条件,例如:

 printf("Enter today's condition: (1: sunny, 2: cloudy, 3: precipitation"); 

然后检查条件并进行计算。

尝试这个;

#include <stdio.h>
int main(void) {

    int days;
    char condt;
    double high,low,aver;

    printf("Weather Analyzer \n");
    printf("================ \n");

    printf("Please enter a positive number of days:");
    scanf("%d", &days);



    if (days <= 0) {

            while (days <= 0) {
                    printf("Please enter a positive number of days:");
                    scanf("%d", &days);
            }
    }



    printf("Enter today's high:");
    scanf("%lff", &high);

    printf("Enter today's low:");
    scanf("%lff", &low);

    printf("Enter today's condition: (s: sunny, c: cloudy, p: precipitation");
    scanf(" %c", &condt);

    aver = high + low / 2.0;

    printf("Today's average temperature is: %llf \n", aver);
    system("pause");
}

暂无
暂无

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

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