提示用户在 C 中再次播放
我已经在另一篇文章中询问过这里,但没有一个答案对我的程序有帮助。我有一个程序,要求用户提供数字并计算平均值、中位数和众数。然后程序应该提示用户再次玩,如果用户选择 y 或 Y 则应重玩游戏,n 或 N 停止,如果除此之外的其他内容,请说无效,请键入 y 或 n 到 bla bla你明白了。这是我的主要方法,我的方法 goAgain() :
#define MAX 25
#include<stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
int readTotalNums();
void fillArray(int total, int nums[]);
void sortArray(int nums[], int total);
double findMean(int nums[], int total);
double findMedian(int nums[], int total);
int findMode(int nums[], int total);
void printResults(double mean, double median, double mode);
bool goAgain();
int main() {
int nums[MAX];
int total;
double mean, median, mode;
bool b;
do {
total = readTotalNums();
fillArray(total, nums);
sortArray(nums, total);
mean = findMean(nums, total);
median = findMedian(nums, total);
mode = findMode(nums, total);
printResults(mean, median, mode);
b = goAgain();
} while (b==true);
return 0;
}
//每次我玩游戏时,这里都有其他方法
bool goAgain() {
char *temp;
printf("\nWould you like to play again(Y/N)? ");
scanf("%c", &temp);
while (temp != 'n' && temp != 'N' && temp != 'y' && temp != 'Y') {
printf("\nI am sorry that is invalid -- try again");
printf("\nWould you like to play again(Y/N)? ");
scanf("%c", &temp);
}
if (temp == 'y' || temp == 'Y') {
return true;
} else {
return false;
}
}
,它会出现提示,我输入的任何内容都不会执行任何操作,并且一直显示无效,请重试,即使输入是 ay 还是 N。感谢您的帮助:)
I've already asked on here in another post, but none of the answers helped my program. I have a program that asks the user for numbers and calculates the mean, median, and mode. Then the program should prompt the user to play again, and if the user selects y or Y it should replay the game, n or N to stop, and if something else other than that, say invalid and please type y or n to bla bla you get it. here is my main, and my method goAgain() :
#define MAX 25
#include<stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
int readTotalNums();
void fillArray(int total, int nums[]);
void sortArray(int nums[], int total);
double findMean(int nums[], int total);
double findMedian(int nums[], int total);
int findMode(int nums[], int total);
void printResults(double mean, double median, double mode);
bool goAgain();
int main() {
int nums[MAX];
int total;
double mean, median, mode;
bool b;
do {
total = readTotalNums();
fillArray(total, nums);
sortArray(nums, total);
mean = findMean(nums, total);
median = findMedian(nums, total);
mode = findMode(nums, total);
printResults(mean, median, mode);
b = goAgain();
} while (b==true);
return 0;
}
//other methods here
bool goAgain() {
char *temp;
printf("\nWould you like to play again(Y/N)? ");
scanf("%c", &temp);
while (temp != 'n' && temp != 'N' && temp != 'y' && temp != 'Y') {
printf("\nI am sorry that is invalid -- try again");
printf("\nWould you like to play again(Y/N)? ");
scanf("%c", &temp);
}
if (temp == 'y' || temp == 'Y') {
return true;
} else {
return false;
}
}
every time I play the game, and it gets to the prompt, anything I type it just does nothing, and keeps saying invalid try again, even if the input is a y or N. thanks for your help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
char *temp;
应为char temp;
char *temp;
should bechar temp;
不要将
temp
声明为指针,而且您还没有为其分配内存。相反,将您的声明更改为
char temp;
Don't declare
temp
as a pointer, also you have not allocated memory for it.Instead change your declaration to
char temp;
既然你已经有了正确的答案,就不用再说了,所以我会添加一个小提示。
since you already have the right answer, no need to say that again, so I'll add a small tip.