在点之后,我如何减少小数数并修复显示很多单词的错误?
首先,我的代码询问您的名字,并显示一个随机数,您需要提取其平方根。因此,如果代码显示81
,则需要键入9
。但是,当数字为十进制(例如1.54896
)时,我尝试在发生这种情况时尝试编写,您只需要在点之后放两个数字,但行不通。当您编写错误的答案时,它显示了很多次相同的单词,直到停止代码为止。我该如何解决?我很新。
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h> /* a função rand precisa dessa biblioteca*/
#include <math.h>
#include <time.h>
int main()
{
/* nome */
string nome = get_string("olá, qual é o seu nome? aaaaa ");
int a, c;
/* os valores q podem ser formados, até 10 valores */
printf("Olá, %s. Qual é a raiz quadrade de \n", nome);
srand(time(NULL));
for (a = 0; a < 1; a++)
/* o valor a ser gerado */
c = 10 rand() % 100 + 1;
printf("%d ", c);
/* o n é a resposta */
int n;
/* o b é o resultado, entao sua reposta(n) for igual ao resultado certo(b), vc ganha um parabéns */
float b;
scanf("%d", &n);
/* b é a raiz de a */
b = sqrt(c);
if (b == "%d")
{
("%0.2f", n);
}
else (n == b)
{
printf("parabéns, vc tem dois neurônios.\n");
}
else
{
while (n > b || n < b)
{
printf("tente de novo.\n");
n++;
}
}
return 0;
}
当您错误的答案时:
gcc loop2.c -o loop2teste20 -lm -lcs50
./loop2teste20
olá, qual é o seu nome? teste
Olá, teste. Qual é a raiz quadrada de
71 8,42
tente de novo.
tente de novo.
tente de novo.
...
First, my code asks your name and it shows a random number and you need to extract the square root of it. So if the code shows 81
, you need to type 9
. But, when the number is decimal (1.54896
for example), I tried to write when this occurs you just need to put the two digits after the point, but didn't work. And when you write the wrong answer, it shows many times the same word until you stop the code. How can I fix this? I am very new.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h> /* a função rand precisa dessa biblioteca*/
#include <math.h>
#include <time.h>
int main()
{
/* nome */
string nome = get_string("olá, qual é o seu nome? aaaaa ");
int a, c;
/* os valores q podem ser formados, até 10 valores */
printf("Olá, %s. Qual é a raiz quadrade de \n", nome);
srand(time(NULL));
for (a = 0; a < 1; a++)
/* o valor a ser gerado */
c = 10 rand() % 100 + 1;
printf("%d ", c);
/* o n é a resposta */
int n;
/* o b é o resultado, entao sua reposta(n) for igual ao resultado certo(b), vc ganha um parabéns */
float b;
scanf("%d", &n);
/* b é a raiz de a */
b = sqrt(c);
if (b == "%d")
{
("%0.2f", n);
}
else (n == b)
{
printf("parabéns, vc tem dois neurônios.\n");
}
else
{
while (n > b || n < b)
{
printf("tente de novo.\n");
n++;
}
}
return 0;
}
And when you put the wrong answer:
gcc loop2.c -o loop2teste20 -lm -lcs50
./loop2teste20
olá, qual é o seu nome? teste
Olá, teste. Qual é a raiz quadrada de
71 8,42
tente de novo.
tente de novo.
tente de novo.
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有多个问题:
for(a = 0; a&lt; 1; a ++)
仅迭代一次,不是很有用,在最小示例中绝对不需要。c = 10 rand()%100 + 1;
是语法错误。使用c = rand()%100 + 1;
scanf(“%d”,&amp; n)
如果输入不以数字开头。您应该测试返回值并投诉如果输入无效。如果(b ==“%d”)
是毫无意义的:b
是float
,您无法将其与字符串进行比较。目前尚不清楚您试图通过此测试实现什么。(“%0.2F”,n)
什么都不做。您的意思是printf(“%0.2F”,n)
?这也是不正确的,因为n
是int
和%0.2F
期望float
或double
。printf(“Parabéns,Vc tem doisneurônios。\ n”);
希望用户有一种幽默感:)else(n == b)
>是语法错误,如果(n == b)while(n&gt; b |)。 |。 }
如果b
不是整数,则直到n
达到int_max
,并且您获得未定义的行为。这说明了为什么您会重复输出Tente de Novo。
这是一个修改版本:
输出:输出:
There are multiple problems in your code:
for (a = 0; a < 1; a++)
just iterates once, not very useful and definitely not needed in a minimal example.c = 10 rand() % 100 + 1;
is a syntax error. Usec = rand() % 100 + 1;
scanf("%d", &n)
may fail if the input does not start with a number. You should test the return value and complain if input is invalid.if (b == "%d")
is meaningless:b
is afloat
, you cannot compare that to a string. It is unclear what you are trying to achieve with this test.("%0.2f", n)
does not do anything. Did you meanprintf("%0.2f", n)
? This would be incorrect too asn
is anint
and%0.2f
expects afloat
or adouble
.printf("parabéns, vc tem dois neurônios.\n");
Let's hope the user has a sense of humor :)else (n == b)
is a syntax error, you probably meanelse if (n == b)
while (n > b || n < b) { n++; }
runs forever ifb
is not an integer, untiln
reachesINT_MAX
and you get undefined behavior. This explains why you get repeated output oftente de novo.
Here is a modified version:
Output: