[英]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>
這里high
和low
是double
。 將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.