简体   繁体   English

For 循环读取和打印值

[英]For-loops to read and print values

I am using for-loops to read and print the values.我正在使用 for 循环来读取和打印值。 As you can see it stores only the last input.如您所见,它仅存储最后一个输入。 Any suggestions?有什么建议?

#include <stdio.h>
int main()
{
    int i;
    for(int a = 0; a < 5; a ++)
    {
        printf("Enter your age: ");
        scanf("%d", &i);
    }
    for(int b = 0; b < 5; b ++)
    {
        printf("Hi I'm %d years old\n", i);
    }
    return 0;
}

And here's the output .这是输出。 . .

Enter your age: 11
Enter your age: 22
Enter your age: 33
Enter your age: 44
Enter your age: 55
Hi I'm 55 years old
Hi I'm 55 years old
Hi I'm 55 years old
Hi I'm 55 years old
Hi I'm 55 years old

Your problem is that your are only storing the last answer you receive.您的问题是您只存储收到的最后一个答案。 The simplest solution that I can give you is to use array:我可以给你的最简单的解决方案是使用数组:

#include <stdio.h>
int main()
{
    int ages[5];
    for(int i = 0; i < 5; i++)
    {
        printf("Enter your age: ");
        scanf("%d", &ages[i]);
    }
    for(int i = 0; i < 5; i++)
    {
        printf("Hi I'm %d years old\n", ages[i]);
    }
    return 0;
}

but really, pick up one of those recommend books and read a bit more但真的,拿起其中一本推荐的,多读一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM