我是 C 语言的初学者,遇到一个很大的 while 循环问题。基本上我在循环中有 2 个变量赋值,由于某种原因不会在每次迭代时执行,所以变量的值保持不变。除此之外,一切正常。
编辑:我现在知道我在 play() 函数中设置为 double 的变量默认为 int 。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int play();
int main()
{
double correct, attempts;
char host[10], player[10];
system("cls");
printf("What's up strangers! Welcome to my game.\n");
printf("Please CAREFULLY read all prompts from this point forward.\n");
system("pause");
system("cls");
printf("First thigs first, HOST, what is your name? ");
scanf("%s", host);
system("cls");
printf("Ok %s, what is the number you would like the player to guess? ",
host);
scanf("%d", &correct);
printf("Then how many attempts would like them to have on guessing? ");
scanf("%d", &attempts);
printf("Thank you %s, now please leave room for our fearless player of
the day.\n", host);
system("pause");
system("cls");
printf("Mighty player, your fearless game host for today is %s. Please
enter your name: ", host);
scanf("%s", player);
system("cls");
printf("Alright %s! The rule is simple, you have %d attempts to guess
%s's number.", player, attempts, host);
printf("Any time you guess wrong, the game will tell you how far off your
answer was.\n");
printf("Your score will be displayed at he end of the game.\n");
printf("Whenever you are ready... Good luck!");
system("pause");
return play(correct, attempts, host, player);
}
play(correct, attempts, host, player)
{
double count = 1, pcent, pdiff, answer;
char score[1], suffix[2];
while (count < attempts)
{
pcent = 100*sqrt(pow((1-(count/attempts)), 2));
if (pcent == 100)
{
strcpy(score, "S");
}
else if (pcent < 100 && pcent >= 90)
{
strcpy(score, "A");
}
else if (pcent < 90 && pcent >= 80)
{
strcpy(score, "B");
}
else if (pcent < 80 && pcent >= 70)
{
strcpy(score, "C");
}
else if (pcent < 70 && pcent >= 60)
{
strcpy(score, "D");
}
else
{
strcpy(score, "F");
}
if (count == 1)
{
strcpy(suffix, "st");
}
else if (count == 2)
{
strcpy(suffix, "nd");
}
else if (count == 3)
{
strcpy(suffix, "st");
}
else
{
strcpy(suffix, "th");
}
printf("%d", pcent);
printf("%s, please enter your %d%s guess: ", player, count, suffix);
scanf("%d", &answer);
pdiff = 200*((answer-correct)/(answer+correct));
printf("%f", pdiff);
system("cls");
if (answer == correct)
{
printf("That is correct %s! You got it on your %d%s try.\n",
player, count, suffix);
printf("Your score is %s.", score);
system("pause");
return main();
}
else
{
printf("Wrong answer %s! Your guess was %f percent off from the
correct answer.\n", player, pdiff);
printf("You have %d attempts remaining.\n", attempts-count);
system("pause");
}
count ++;
}
printf("Sorry %s, but you have no more attempt remaining... Please play
again later.\n", player);
return main();
}
答案 0 :(得分:1)
如果没有您的完整代码,就不可能 100% 确定我已经涵盖了所有内容,但我相信我已经捕捉到了明显的内容:
pcent = 100*sqrt(pow((1-(count/attempts)), 2));
和 pdiff = 200*((answer-correct)/(answer+correct));
中的整数除法有关。如果您需要每个除法中的小数部分,请将除数(或被除数)转换为 (double)
。strcpy()
给它们的字符串。建议将 score
设为 char
并使用 suffix[8]
。 (最好是 10,000 个字符太长而不是一个字符太短)。break;
情况下提供一个 switch()
语句(尽管您现在已经编辑了一个使用过的 if .. else if .. else
)。如果您未能break;
失败,则会出现下一个案例。int
,因此当您 return
时,您 return value;
其中 value
是类型 int
,而不是 main()
。您不需要返回调用函数,因为您的 play()
函数已经将 frame-pointer
的 main()
作为 函数序言 的一部分存储并且知道从哪里返回控制何时完成。return 1;
错误和 return 0;
成功,C 为 EXIT_FALURE
(1
) 和 EXIT_SUCCESS
( 0
) 提供了两个宏。立>
fgets()
而不是 scanf()
获取所有用户输入,因此 stdin
中未读的内容不依赖于转换说明符或是否发生匹配失败。sscanf()
从缓冲区获取(解析)单个值,就像使用 scanf()
一样,但提供缓冲区作为第一个参数。printf()
(或 puts()
或 fputs()
)即可输出连续的文本块——无论它有多少行。printf()
,否则使用 puts()
如果您希望自动附加 '\n'
,如果需要行尾控制则使用 fputs()
- - 例如提示输入。"The expression of each case label shall be an integer constant expression..."
C11 Standard - 6.8.4.2 The switch statement(p3) 您对范围表达式的使用,例如60 ... 69
仅由非标准编译器扩展提供。可能还有更多我忘记提及的更改。我怀疑您的 player
实际上是 char*
,但由于缺少类型,它在您的问题中默认为 int
。因此,如果是这样,请将 %d
格式字符串中使用的 printf()
更改为 %s
。我在下面加入了进一步的评论:
更新功能
#define MAXC 256 /* if you need a constant, #define one or more */
int play (int correct, int attempts, int host, int player)
{
int count = 1, answer = 0; /* initialize variables */
char buf[MAXC] = "", score = 0, suffix[8] = "";
double pdiff = 0., pcent = 0.;
while (count < attempts)
{
pcent = 100 * sqrt(pow((1-((double)count/attempts)), 2));
switch ((int)pcent)
{
case 60 ... 69:
score = 'D'; break; /* you must break or fall-through occurs */
case 70 ... 79:
score = 'C'; break;
case 80 ... 89:
score = 'B'; break;
case 90 ... 99:
score = 'A'; break;
case 100:
score = 'F'; break;
default:
score = 'S'; break;
}
switch (count)
{
case 1:
strcpy (suffix, "st"); break; /* ditto */
case 2:
strcpy (suffix, "nd"); break;
case 3:
strcpy (suffix, "rd"); break;
default:
strcpy (suffix, "th"); break;
}
printf ("percent: %.2f\n", pcent); /* left alone since it looks like debug */
printf ("%d, please enter your %d%s guess: ", player, count, suffix);
if (!fgets (buf, MAXC, stdin)) { /* read & VALIDATE all user-input */
puts ("(user canceled)");
return 1;
}
if (sscanf (buf, "%d", &answer) != 1) { /* validate EVERY conversion */
fputs ("error: invalid integer input.\n", stderr);
return 1;
}
pdiff = 200 * ((double)(answer-correct) / (answer+correct));
printf ("%f", pdiff);
// system("cls"); /* not really needed, just scrolls lines up */
if (answer == correct)
{
printf ("That is correct %d! You got it on your %d%s try.\nYour score is %c.",
player, count, suffix, score);
// system("pause");
fgets (buf, MAXC, stdin); /* will do nicely instead of pause */
return 1; /* just return */
}
else
{
printf ("Wrong answer %d! "
"Your guess was %.2f percent off from the correct answer.\n"
"You have %d attempts remaining.\n", player, pdiff, attempts-count);
// system("pause");
fgets (buf, MAXC, stdin); /* will do nicely instead of pause */
}
count++;
}
printf ("Sorry %d, but you have no more attempt remaining... "
"Please play again later.\n", player);
return 0; /*just return -- the function handles saving the callers */
}
这是我对您需要的最佳猜测。根据需要调整 player
的类型。
答案 1 :(得分:0)
好吧,wismus2999,
我相信如果变量是整数类型,你可能不会修改它们,一个例子是:
while (<condition>) {
a = 2;
}
上面的代码与此不同:
while (<condition>) {
a++;
// or a = a + 1;
}
由于没有代码或照片可以分析,所以很难给出一个 具体答案。