C 由于此函数,我的do while循环是否不工作?
我正在制作一个程序,让用户猜一个计算机正在思考的数字1-100 在节目结束时,当用户猜到正确的数字时,我试图让节目询问用户是否想再次播放(重新启动节目) 为了解决这个问题,我尝试使用C 由于此函数,我的do while循环是否不工作?,c,function,loops,while-loop,C,Function,Loops,While Loop,我正在制作一个程序,让用户猜一个计算机正在思考的数字1-100 在节目结束时,当用户猜到正确的数字时,我试图让节目询问用户是否想再次播放(重新启动节目) 为了解决这个问题,我尝试使用do while循环&char repeat。这个循环几乎从项目开始一直延伸到项目结束,尽管没有成功。有人知道我做错了什么吗?是否因为函数talfunktion,循环无法通过 代码: #包括 #包括 #包括 int talfunktion(int-tal、int-guess、int-try、char-repeat);
do while
循环&char repeat代码>。这个循环几乎从项目开始一直延伸到项目结束,尽管没有成功。有人知道我做错了什么吗?是否因为函数talfunktion
,循环无法通过
代码:
#包括
#包括
#包括
int talfunktion(int-tal、int-guess、int-try、char-repeat);
int main(){
做{
srand(时间(空));
int tal=rand()%100+1;//tal是代码考虑的正确值
int guess;//guess是用户的猜测值
int trytes=0;//在得到正确结果之前所做的尝试次数
字符重复;
printf(“Psst,正确的数字是:%d\n”,tal);//稍后删除,与uppg无关。
printf(“我想的是一个介于1和100之间的数字,猜猜是哪个!”;
printf(“\n输入:”);
scanf(“%d”,猜测(&guess);
猜=talfunktion(tal,猜,尝试,重复);
getchar();
getchar();
返回0;
}
int-talfunktion(int-tal、int-guess、int-try、char-repeat){
做{
如果(猜测总计){
尝试++;
printf(“\n您的猜测太高,请再试一次!”);
printf(“\n输入:”);
scanf(“%d”,猜测(&guess);
}
}而(猜>tal | |猜
评论中提到了一些描述问题的东西,
你应该看的东西:
- 不要在另一个函数中定义函数
- 在放置返回语句时要小心
- 使用字符测试时,使用变量的
char
type
- 考虑简化逻辑比较。(例如
guess>tal | | guess
与guess!=tal
相同)
- 确保自动变量的放置位置使其在使用位置可见李>
- 在格式说明符中放置空格:
%c“
,以便scanf()
使用换行符。(而不是过度使用getchar()
)
这是您的代码的简化版本,带有修改的main
和talfunktion
函数
char talfunktion(int tal);
int main (void) {
int tal=0;//remove from inside {...} to make it visible to rest of function
char repeat = 'n';
srand(time(NULL));
tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of
do {
repeat = talfunktion(tal);
}while((tolower(repeat) == 'y'));
return 0;
}
char talfunktion(int tal)//do all relevant work in function and return
{ //only what is necessary
int guess = 0;
char repeat = 'n';
printf("Im thinking of a number between 1 and 100, guess which!");
printf("\nEnter a number from 1 to 100: ");
scanf("%d", &guess);
if((guess < 1) || (guess > 100))
{
printf("Entered out of bounds guess...\n");
}
else if (guess != tal)
{
if(guess < tal) printf("guess too small\n");
else printf("guess too large\n");
printf("Try again? <'n' or 'y'>\n");
scanf(" %c", &repeat);//space in format specifier to consume newline character
if(tolower(repeat) != 'y') return 'n';//tolower() allows both upper and lower case
}
else
{
printf("Congratulations: You guessed right.\n");
printf("Play again? <'n' or 'y'>\n");
scanf(" %c", &repeat);
}
return repeat;
}
char-talfunktion(int-tal);
内部主(空){
int tal=0;//从{…}内部删除,使其对函数的其余部分可见
字符重复='n';
srand(时间(空));
tal=rand()%100+1;//tal是代码考虑的正确值
做{
重复=talfunktion(tal);
}而((tolower(repeat)='y');
返回0;
}
char-talfunktion(int-tal)//在函数和返回中执行所有相关工作
{//仅限于必要的内容
int guess=0;
字符重复='n';
printf(“我想的是一个介于1和100之间的数字,猜猜是哪个!”;
printf(“\n输入一个从1到100的数字:”);
scanf(“%d”,猜测(&guess);
如果((猜测<1)| |(猜测>100))
{
printf(“输入越界猜测…\n”);
}
否则如果(猜测!=tal)
{
如果(猜测
这是一种可能的解决方案
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void talfunktion(int tal, int guess, int* tries)
{
if (guess < tal)
{
(*tries)++;
printf("\nYour guess is too low, try again!");
}
else if (guess > tal)
{
(*tries)++;
printf("\nYour guess is too high, try again!");
}
else if (guess == tal)
{
(*tries)++;
printf("\nCongratulations, that is correct!");
printf("\nYou made %d attempt(s)", *tries);
}
}
int main (void)
{
int tal; //tal is the correct value that the code is thinking of
int guess; //guess is the guessed value of the user
int tries = 0; // amount of tries it took until getting correct
char playAgain;
do {
srand(time(NULL));
tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of
printf("\nIm thinking of a number between 1 and 100, guess which!");
printf("\nEnter: ");
scanf("%d", &guess);
talfunktion(tal, guess, &tries);
printf("\nPsst, the right number is: %d", tal); // remove later, not relevant to uppg.
getchar(); //to halt the code for taking the input
if(guess == tal)
{
tries = 0;
printf("\nPlay Again? (y/n)\n");
scanf("%c", &playAgain);
}
} while (playAgain != 'n');
return 0;
}
#包括
#包括
#包括
无效talfunktion(int-tal、int-guess、int*Trys)
{
如果(猜测总计)
{
(*尝试)+;
printf(“\n您的猜测太高,请再试一次!”);
}
else if(猜测=tal)
{
(*尝试)+;
printf(“\n语法,这是正确的!”);
printf(“\n您尝试了%d次”,*次尝试);
}
}
内部主(空)
{
int-tal;//tal是代码考虑的正确值
int guess;//guess是用户的猜测值
int trytes=0;//在得到正确结果之前所做的尝试次数
再次播放;
做{
srand(时间(空));
tal=rand()%100+1;//tal是代码考虑的正确值
printf(“\nIm想到一个介于1和100之间的数字,猜猜哪一个!”);
printf(“\n输入:”);
scanf(“%d”,猜测(&guess);
talfunktion(tal、猜测和尝试);
printf(“\nPsst,正确的数字是:%d”,tal);//稍后删除,与uppg无关。
getchar();//停止获取输入的代码
如果(猜测=tal)
{
尝试=0;
printf(“\n再次显示?(y/n)\n”);
scanf(“%c”,再次播放(&P);
}
}while(再次播放!=“n”);
返回0;
}
您有一个返回0代码>在循环中,因此它永远不会循环。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void talfunktion(int tal, int guess, int* tries)
{
if (guess < tal)
{
(*tries)++;
printf("\nYour guess is too low, try again!");
}
else if (guess > tal)
{
(*tries)++;
printf("\nYour guess is too high, try again!");
}
else if (guess == tal)
{
(*tries)++;
printf("\nCongratulations, that is correct!");
printf("\nYou made %d attempt(s)", *tries);
}
}
int main (void)
{
int tal; //tal is the correct value that the code is thinking of
int guess; //guess is the guessed value of the user
int tries = 0; // amount of tries it took until getting correct
char playAgain;
do {
srand(time(NULL));
tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of
printf("\nIm thinking of a number between 1 and 100, guess which!");
printf("\nEnter: ");
scanf("%d", &guess);
talfunktion(tal, guess, &tries);
printf("\nPsst, the right number is: %d", tal); // remove later, not relevant to uppg.
getchar(); //to halt the code for taking the input
if(guess == tal)
{
tries = 0;
printf("\nPlay Again? (y/n)\n");
scanf("%c", &playAgain);
}
} while (playAgain != 'n');
return 0;
}