【发布时间】:2015-07-05 22:27:12
【问题描述】:
我调试了程序,发现虽然你输入了一个选项,比如Y/y/n/N或者H/h/l/L,但是if函数不能识别输入,就跳过了。感谢您的任何建议! RNG 工作正常,对我来说一切都很好。
#include <stdio.h>
#include <time.h>
int main(){
char startAnswer[1];
char gameAnswer[1];
int i;
int r;
int t;
int compare;
srand(time(NULL));
printf("Do you want to play a game? Y/N. \n");
scanf(" %c", &startAnswer);
start:
if (startAnswer == 'y' || 'Y'){
goto game;
}
else if (startAnswer == 'n' || 'N'){
printf("Game will now exit.\n");
}
else {
printf("You entered an invalid answer, try again.\n");
goto start;
}
game:
printf("There will be ten numbers calculated.\n");
printf("Try to guess what the numbers are before\n");
printf("they are displayed. One at a time.\n\n");
for(i = 0; i < 12; i++){
randGen:
r = ( rand() % 9 ) + 1;
t = ( rand() % 9 ) + 1;
if(t == r){
goto randGen;
}
printf("Is the next number higher or lower than %d\n\n" , r);
scanf(" %c" , &gameAnswer);
//ignore (r > t) ? (compare = 0) : (compare = 1);
if (r > t){
compare = 1;
}
else{
compare = 0;
}
if (gameAnswer == ('H' || 'h')){
if (compare == 1){
printf("You win this round!");
goto randGen;
}
}
else if (gameAnswer == ('L' || 'l')){
if (compare == 0){
printf("You win this round!");
goto randGen;
}
}
else if (gameAnswer != ('l' || 'L' || 'h' || 'H')){
if (compare == 0){
printf("Please Enter H/L for higher/lower answer.\n\n");
goto randGen;
}
}
else{
printf("You lost this round.");
goto randGen;
}
r = t;
}
}
【问题讨论】:
-
没有您的代码,我们根本无法帮助您。
-
这不是正确的提问方式。请给我们看一些代码示例或者你做了什么。
-
请提供有此问题的演示代码。
-
用户输入n/N时,您不会退出游戏。
-
为什么人们总是假设计算机正在神奇地“跳过”他们的状况?
标签: c arrays if-statement char logical-operators