我正在尝试制作Magic 8 Ball程序,但是我的循环有一个错误。
如果再次播放,循环会混乱,其行为就像您一直按Enter键约三个周期,直到它再次起作用。
此外,输入问题时会有延迟。您必须按两次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;
}
}
答案 0 :(得分:4)
解决了您的问题。添加了注释说明。希望我能抓住一切。测试了程序,现在可以正常工作了。
#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;
}
}
答案 1 :(得分:-1)
您可以尝试
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void ans(int x);
int main()
{
srand(time(0));
int num,i;
do{
num = rand()%10;
char question[1000];
puts("MAGIC 8 BALL!");
puts("Press Enter When Ready.");
puts("Input Yes or No questions only!\n");
scanf("%s",question);
printf("\nTHE ANSWER | ");
ans(num);
printf("\n\n");
printf("Press any key to try again.\nPress [x] to Exit.\n\n");
char c = getchar();
c=getchar();
if(c=='x'){
break;
}
}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;
}
}