代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int input;
system("clear");
printf("Welcome to Riddles! A game of mystery. Press any key and the enter button to continue.\n");
scanf("%d", &input);
system("clear");
sleep(1);
printf("Riddle 1. At night they come without being told. By day they are gone without being stolen. What are they?\n");
printf("1. goats\n");
printf("2. pillows\n");
printf("3. memories\n");
printf("4. the stars\n");
scanf("%d", input);
if (input == 1)
{
system("clear");
sleep(1);
printf("Correct. The stars is the answer\n");
}
if (input != 1)
{
sleep(1);
system("clear");
printf("Incorrect Game over\n");
}
}
问题:输出将打印:Welcome to Riddles!一场神秘的游戏。按任意键和回车按钮继续”就像应该的那样,但是当我输入字母并按回车键时,会出现以下内容:
谜语 1. 晚上他们不经通知就来了。白天它们就消失了,没有被偷。这些是什么? 1. 山羊 2.枕头 3.记忆 4.星星
游戏结束错误
问题是,我什至无法输入答案。它将跳到第二个 if 语句而不接受任何输入。有谁知道问题出在哪里吗?
(附:我从《指环王北方 war 》中得到了这个谜语)
最佳答案
此声明中有一个拼写错误
scanf("%d", input);
应当
scanf("%d", &input);
此外,您不能为 int 类型的对象输入字母。因此,更改第一个 scanf 以便您可以输入字母。
关于c - 为什么我的第一个 if 语句被跳过并且不接受 C 中的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26832537/