如何在C中使用带有用户输入的循环函数?

Mar*_*ite 1 c loops

这是我创建的平均程序.

#include <stdio.h>
#include <stdlib.h>

 int main()
 {
    float f1,f2,f3;

    /* Program to calculate averages. */

    /*Asks for the numbers.*/

    printf(" Please enter three numbers.\n");
    printf ("\t" "First number please.\n");
    scanf("%f", &f1);
    printf ("\t" "Second number please.\n");
    scanf ("%f", &f2);
    printf("\t" "Third number please.\n");
    scanf("%f", &f3);

    /* Now it averages it.*/
    printf(" Thank you, wait one.\n");
    printf(" Excellent, your sum is.\n");
    printf("%f""\n", f1+f2+f3);


    printf("Your average of the sum is now!!!!\n");
    printf("%f", (f1+f2+f3)/3);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我会把它变成干活吗?还是一个if else?

Jon*_*ler 5

如果要重复整个条目和平均过程,可以围绕代码循环:

#include <stdio.h>

int main(void)
{
    float f1,f2,f3;

    while (1)
    {
        printf("Please enter three numbers.\n");
        printf("\tFirst number please.\n");
        if (scanf("%f", &f1) != 1)
            break;
        printf("\tSecond number please.\n");
        if (scanf("%f", &f2) != 1)
            break;
        printf("\tThird number please.\n");
        if (scanf("%f", &f3) != 1)
            break;

        printf("Your sum     is %f\n", f1+f2+f3);
        printf("Your average is %f\n", (f1+f2+f3)/3);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请注意,此代码会在scanf()每次使用时检查返回值,如果出现问题则会中断循环.不需要字符串连接,单个printf()字符串肯定可以打印字符串和值.

这是一个简单的第一阶段; 有更复杂的技术可以使用.例如,您可以创建一个函数来提示和读取数字:

#include <stdio.h>

static int prompt_and_read(const char *prompt, float *value)
{
    printf("%s", prompt);
    if (scanf("%f", value) != 1)
        return -1;
    return 0;
}

int main(void)
{
    float f1,f2,f3;

    while (printf("Please enter three numbers.\n") > 0 &&
           prompt_and_read("\tFirst number please.\n", &f1) == 0 &&
           prompt_and_read("\tSecond number please.\n", &f2) == 0 &&
           prompt_and_read("\tThird number please.\n", &f3) == 0)
    {
        printf("Your sum     is %f\n", f1+f2+f3);
        printf("Your average is %f\n", (f1+f2+f3)/3);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果你想远离一组固定的三个值,那么你可以迭代直到遇到EOF或错误:

#include <stdio.h>

static int prompt_and_read(const char *prompt, float *value)
{
    printf("%s", prompt);
    if (scanf("%f", value) != 1)
        return -1;
    return 0;
}

int main(void)
{
    float value;
    float sum = 0.0;
    int   num = 0;

    printf("Please enter numbers.\n");

    while (prompt_and_read("\tNext number please.\n", &value) == 0)
    {
        sum += value;
        num++;
    }

    if (num > 0)
    {
        printf("You entered %d numbers\n", num);
        printf("Your sum     is %f\n", sum);
        printf("Your average is %f\n", sum / num);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您可能还决定使用空格替换提示字符串末尾的换行符,以便在与提示符相同的行中键入该值.

如果要检查是否重复计算,可以在代码的第一个或第二个版本上使用次要变量:

#include <stdio.h>

static int prompt_and_read(const char *prompt, float *value)
{
    printf("%s", prompt);
    if (scanf("%f", value) != 1)
        return -1;
    return 0;
}

static int prompt_continue(const char *prompt)
{
    printf("%s", prompt);
    char answer[2];
    if (scanf("%1s", answer) != 1)
        return 0;
    if (answer[0] == 'y' || answer[0] == 'Y')
    {
        int c;
        while ((c = getchar()) != EOF && c != '\n')      // Gobble to newline
            ;
        return 1;
    }
    return 0;
}

int main(void)
{
    float f1,f2,f3;

    while (printf("Please enter three numbers.\n") > 0 &&
           prompt_and_read("\tFirst number please.\n", &f1) == 0 &&
           prompt_and_read("\tSecond number please.\n", &f2) == 0 &&
           prompt_and_read("\tThird number please.\n", &f3) == 0)
    {
        printf("Your sum     is %f\n", f1+f2+f3);
        printf("Your average is %f\n", (f1+f2+f3)/3);
        if (prompt_continue("Do you want to try again?") == 0)
            break;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)