【发布时间】:2021-04-01 18:16:47
【问题描述】:
我是 C 语言的初学者,遇到了一个大的 while 循环问题。基本上我在循环中有 2 个变量赋值,由于某种原因在每次迭代中都没有执行,所以变量的值保持不变。除此之外,一切正常。
编辑:我现在知道我设置为 double 的变量在 play() 函数中默认为 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();
}
【问题讨论】:
-
我的水晶球说删除
while (stuff);之后的';'...欢迎来到Stack Overflow。请尽快阅读About 页面并访问描述How to Ask a Question 和How to create a Minimal, Complete, and Verifiable example (MCVE) 的链接。提供必要的详细信息,包括您的 MCVE、编译器警告和相关错误(如果有),将允许这里的每个人帮助您解决您的问题。 -
char score[1], suffix[2];都是 1 字符太短,无法容纳您复制的字符串。您必须至少拥有char score[2], suffix[3];。建议两者都使用char score[8], suffix[8];,或者将score设为char而不是数组。
标签: c while-loop variable-assignment