我正在尝试制作一个 Magic 8 Ball 程序,但我的循环有一个错误。
如果您再次播放,循环会变得困惑,并且表现得就像您一直按回车键大约三个周期,直到它再次工作为止。
此外,您输入问题时会出现延迟。您必须按两次 Enter 才能得到答案。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
void ans(int x);
int main()
{
system("color 0A");
srand(time(0));
int num,i;
do{
char question[100] = {" "};
num = rand()%10;
puts("MAGIC 8 BALL!");
puts("Press Enter When Ready.");
getch();
system("cls");
puts("Input Yes or No questions only!");
printf("\n");
scanf("%s",&question);
getch();
printf("\nTHE ANSWER | ");
ans(num);
getch();
system("cls");
printf("Press any key to try again.\nPress [x] to Exit.");
if(getch()=='x'){
system("cls");
break;
}
system("cls");
}while(1);
}
void ans(int x){
switch(x){
case 1 : printf("YES!");break;
case 2 : printf("NO!");break;
case 3 : printf("It's a thumbs down.");break;
case 4 : printf("Positive!");break;
case 5 : printf("As I see it Yes.");break;
case 6 : printf("Certainly!");break;
case 7 : printf("Negative!");break;
case 8 : printf("Don't Count on it.");break;
case 9 : printf("You don't want to know, trust me.");break;
case 10: printf("I can't say right now.");break;
default : printf("Cannot be determined right now");break;
}
}
最佳答案
解决了您的问题。添加了注释解释。希望我捕获了一切。测试了该程序,目前运行良好。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>
//#include <unistd.h>
#define MAX_QUESTION 1000
#define STATEMENT_AMOUNT 10
int random_num_generator();
void statement_picker(int x);
int main(void) {
char temp;
int rand;
// Your while condition really just needs to be scanning 1 character.
// No point in scanning the entire string, because we don't even use the string
// The scanning jus tells us when to start the loop again
while (1) {
// The introduction of our program
system("cls");
printf("MAGIC 8 BALL!\n");
printf("Press Enter When Ready.\n");
getch();
// Asking user to enter in their question
system("cls");
printf("What is your question?\n");
printf("Only enter yes or no questions!\n");
// You have to flush (aka clear) stdin. Because it uses your previous inputs
// automatically. For example, if you type "I am the best?". The program will not work
// properly for num_of_words - 1 iterations. So 4 - 1. Will not work properly for
// 3 iterations
// Also you don't need to scan in the word, because we don't really care what question
// the user typed. You can save some memory by only scanning in a single character.
// We only use scanf to give a real magic 8 ball experience by stopping and waiting
// for the user to type their question.
scanf("%c", &temp);
fflush(stdin);
// Generating a random number
printf("The answer you seek: ");
rand = random_num_generator();
statement_picker(rand);
// So I noticed here you cls, but you cls without waiting. This the user won't
// be able to read your answer in time. So you should cls after you ask the user
// to quit, or have a timer. Commented a timber for you below. You also need to
// include the unistd.h library to use the sleep function. Just uncomment it
// in the #includes section if you want the timer.
// sleep(10);
// system("cls");
// Asking the user if they want to try again
printf("Would you like to ask another question?\n");
printf("Press [x] to quit\n");
printf("Press any other character to continue\n");
scanf("%c", &temp);
if (temp == 'x') break;
// Flush at the end too, just in case the person types multiple things rather than
// one characters
fflush(stdin);
}
system("cls");
return 0;
}
// Generates a random number between 0 - 10.
/* I noticed in your switch statement you start at case 1. You should start at case 0,
* because you are using the modulus operator Just for example, if rand generators 30 and
* you have 10 cases you will be doing (30 % 10) which is equal to 0 because 10 goes into 10,
* 3 times with no remainders If you really want to start your switch case at 1,
* then you have to plus one to the return value in random_num_generator
*/
int random_num_generator() {
time_t t;
srand((unsigned) time(&t));
return rand() % STATEMENT_AMOUNT;
}
// Picks a statement and prints it, depending on the value of x
void statement_picker(int x){
switch(x){
// Your case should start at 0, not 1 because you will never print out case 0
// the way you had it before.
case 0 : printf("YES!\n");break;
case 1 : printf("NO!\n");break;
case 2 : printf("It's a thumbs down.\n");break;
case 3 : printf("Positive!\n");break;
case 4 : printf("As I see it Yes.\n");break;
case 5 : printf("Certainly!\n");break;
case 6 : printf("Negative!\n");break;
case 7 : printf("Don't Count on it.\n");break;
case 8 : printf("You don't want to know, trust me.\n");break;
case 9 : printf("I can't say right now.\n");break;
// No need for default because you'll never get to it
// (just because of how we implemented this software)
// default : printf("Cannot be determined right now");break;
}
}
关于c - 魔法8球的循环有一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53030443/