【发布时间】:2020-06-21 17:31:15
【问题描述】:
我正在编写一个程序来要求用户输入日期,当用户输入 0/0/0 作为日期时,程序停止要求用户输入日期,但是我的输出有问题,这里是代码:
#include <stdio.h>
#include <stdlib.h>
int main (){
int d, m, y;
for (;d!=0 && m!=0 && y!= 0;){
printf("Enter a date (xx/xx/xxxx): ");
scanf("%d/%d/%d", &d, &m, &y);
}
return 0;
}
运行此程序后,输出为“输入日期(xx/xx/xxxx):”只要 0/0/0 不是输入,则该程序的输出如下所示:
Enter a date (xx/xx/xxxx): 5/5/5
Enter a date (xx/xx/xxxx): 6/6/6
6Enter a date (xx/xx/xxxx): 7/7/7
7Enter a date (xx/xx/xxxx): 4/4/4
7Enter a date (xx/xx/xxxx): 3/3/3
7Enter a date (xx/xx/xxxx):
正如您所看到的输出,问题是在每个换行符上,每个新行上都会显示最大的数字,无论它是否属于 d、m 或 y 变量。 为什么会这样?我该如何解决?
【问题讨论】:
-
您的代码根本不会打印输出。您在向我们展示您的实际代码吗?