【发布时间】:2015-05-28 03:24:48
【问题描述】:
仅限 C 代码:询问用户是否已婚。用户必须输入 0 表示假。用户必须输入任何其他字符为真。只使用一个 printf。
好的,所以我总是将 stackoverflow 作为最后的手段,因为我正在努力解决这个问题。这是我想出的,但我得到了错误,我做了其他事情,比如取出scanf("%f", &t),因为这本质上是不必要的。我还发了char married[3];char married[] =";相反,但这不起作用。
这是我的代码:
#include <stdio.h>
#include <string.h>
int main()
{
char married[3];
unsigned long t;
int f;
scanf("%f", &t);
scanf("%d", &f);
printf(" For the following question: Enter 0 if false. Enter anything but 0 if true. Are you married? %s", married);
if (f == 0)
{
married == "no";
}
else
married == "yes";
return 0;
}
感谢您的帮助。请放轻松我只是学习...
【问题讨论】:
-
married == "no";测试它们的相等性,这永远不会是真的,然后把结果扔掉。这显然不是你想要的。 -
程序需要的输出是什么?无论如何,任何解决方案都可能在
printf之后有scanf。因为当然对用户的提示必须在读取用户输入之前出现。 -
为什么你的程序在它要求用户输入一个数字之前等待用户输入一个数字?
-
为什么在你(尝试)给它一个值之前显示
married? -
每件事的顺序似乎都错了。在尝试设置之前打印
married