在点之后,我如何减少小数数并修复显示很多单词的错误?

发布于 2025-02-13 23:56:36 字数 1504 浏览 0 评论 0原文

首先,我的代码询问您的名字,并显示一个随机数,您需要提取其平方根。因此,如果代码显示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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

究竟谁懂我的在乎 2025-02-20 23:56:36

您的代码中有多个问题:

  • for(a = 0; a&lt; 1; a ++)仅迭代一次,不是很有用,在最小示例中绝对不需要。

  • c = 10 rand()%100 + 1;是语法错误。使用c = rand()%100 + 1;

  • scanf(“%d”,&amp; n)如果输入不以数字开头。您应该测试返回值并投诉如果输入无效。

  • 如果(b ==“%d”)是毫无意义的:bfloat,您无法将其与字符串进行比较。目前尚不清楚您试图通过此测试实现什么。

  • (“%0.2F”,n)什么都不做。您的意思是printf(“%0.2F”,n)?这也是不正确的,因为nint%0.2F期望floatdouble

  • printf(“Parabéns,Vc tem doisneurônios。\ n”);希望用户有一种幽默感:)

  • else(n == b)>是语法错误,如果(n == b)

  • while(n&gt; b |)。 |。 }如果b不是整数,则直到n达到int_max,并且您获得未定义的行为。这说明了为什么您会重复输出Tente de Novo。

这是一个修改版本:

#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    /* nome */
    string nome = get_string("Olá, qual é o seu nome? ");

    srand(time(NULL));

    /* o valor a ser gerado */
    int c = rand() % 100 + 1;

    /* os valores q podem ser formados, até 10 valores */
    printf("Olá, %s. Qual é a raiz quadrade de %d\n", nome, c);

    /* o n é a resposta */
    float n;

    /* o b é o resultado, entao sua reposta(n) for igual ao resultado certo(b), vc ganha um parabéns */

    /* b é a raiz de c */
    float b = sqrtf(c);

    for (int i = 0; i < 10; i++) {
        if (scanf("%f", &n) != 1) {
            printf("not a number\n");
            return 1;
        }
        if (roundf(n * 100) == roundf(b * 100)) {
            /* if the number input is closer than 0.01 */
            printf("parabéns, vc tem dois neurônios.\n");
            break;
        } else {
            printf("tente de novo.\n");
        }
    }
    return 0;
}

输出:输出:

Olá, qual é o seu nome? aaaaa 
Olá, aaaaa. Qual é a raiz quadrade de 90
9.48
tente de novo.
9.49
parabéns, vc tem dois neurônios.

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. Use c = 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 a float, 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 mean printf("%0.2f", n)? This would be incorrect too as n is an int and %0.2f expects a float or a double.

  • 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 mean else if (n == b)

  • while (n > b || n < b) { n++; } runs forever if b is not an integer, until n reaches INT_MAX and you get undefined behavior. This explains why you get repeated output of tente de novo.

Here is a modified version:

#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    /* nome */
    string nome = get_string("Olá, qual é o seu nome? ");

    srand(time(NULL));

    /* o valor a ser gerado */
    int c = rand() % 100 + 1;

    /* os valores q podem ser formados, até 10 valores */
    printf("Olá, %s. Qual é a raiz quadrade de %d\n", nome, c);

    /* o n é a resposta */
    float n;

    /* o b é o resultado, entao sua reposta(n) for igual ao resultado certo(b), vc ganha um parabéns */

    /* b é a raiz de c */
    float b = sqrtf(c);

    for (int i = 0; i < 10; i++) {
        if (scanf("%f", &n) != 1) {
            printf("not a number\n");
            return 1;
        }
        if (roundf(n * 100) == roundf(b * 100)) {
            /* if the number input is closer than 0.01 */
            printf("parabéns, vc tem dois neurônios.\n");
            break;
        } else {
            printf("tente de novo.\n");
        }
    }
    return 0;
}

Output:

Olá, qual é o seu nome? aaaaa 
Olá, aaaaa. Qual é a raiz quadrade de 90
9.48
tente de novo.
9.49
parabéns, vc tem dois neurônios.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文