【发布时间】:2015-08-29 21:11:50
【问题描述】:
我正在关注 youtube 上的教程,并且正在做一个骰子生成器。 它基本上打印出3个骰子结果并总结出骰子结果。 之后,用户将查看总和,并根据总和来猜测下一次滚动是更高、更低还是相同。
下面是我的代码,假设当我输入“是”时,它应该在 if 语句中执行代码。但是,它直接进入了 else 语句。谁能告诉我怎么了?
int answer;
int guess;
int diceRoll4 = 0;
printf("Would you like to guess your next dice? Y/N \n");
scanf(" %c", &answer);
if (answer == 'yes' ){
printf("What is your guess?\n");
printf("please key in your number \n");
scanf(" %d", &guess);
if (guess > diceRoll4 ){
printf(" You got it wrong, too high!");
}
else if (guess < diceRoll4){
printf(" You got it wrong, too low!");
}
else {
printf("You got it right");
}
}
else{
printf("Thanks for playing");
}
【问题讨论】:
-
除其他外,
scanf(...%c..读取单个字符.. -
发布的代码既不能编译也不是完整的程序。请在发布存在运行时问题的代码时。发布干净编译并显示问题的代码。