为什么这个for循环被忽略了?

时间:2016-06-10 20:42:26

标签: c for-loop

我不确定我做错了什么,但for循环没有初始化 代码只是立即显示printfs。因为for循环没有激活

,所以没有值
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("Pause")

main() {
    // INITALIZE VARIABLES 
    int number = 0;
    int i = 0;
    int odd = 0;
    int even = 0;
    int totalNum = 0;
    int tempNum = 0;
    int count;

    printf("Enter a number between 2 and 25\n");
    scanf("%i", &number);

    do{
        if (number < 2 || number > 25)
            printf("That was an invalid number please try again\n");
            scanf("%i", &number);
    } while (number < 2 || number > 25);

    printf("Enter how many numbers you want to input\n");
    scanf("%i", &count);

    for (i = 1; i == count; ++i){
        printf("input numbers\n");
        scanf("%i", &tempNum);
        if (tempNum % 2 == 0)
            even++;
        else
            odd++;
        totalNum = totalNum + tempNum;
    } // END FOR LOOP

    // DISPLAY OUTPUT
    printf("You entered %i numbers\n", count);
    printf("The sum of the %i numbers is %i\n", count, totalNum);
    printf("The average of the %i numbers is %i\n", count, totalNum / count);
    printf("You entered %i odd numbers and %i even numbers\n", odd, even);

    PAUSE;
} // END MAIN

5 个答案:

答案 0 :(得分:1)

当您将count == 1初始化为i1,您的循环最多只会执行一次。

如果您为计数输入1

   printf("Enter how many numbers you want to input\n");
   scanf("%i", &count);

循环将只运行一次,直到i递增到2

你可能想要:

for (i = 1; i <= count; ++i){

答案 1 :(得分:1)

do{
    if (number < 2 || number > 25)
        printf("That was an invalid number please try again\n");
        scanf("%i", &number);
} while (number < 2 || number > 25);

它应该是......

do{
    if (number < 2 || number > 25){
        printf("That was an invalid number please try again\n");
        scanf("%i", &number);
    }
} while (number < 2 || number > 25);

否则它总是要求另一个号码

答案 2 :(得分:0)

i = 1,因此i == count;false,因此会忽略循环。

答案 3 :(得分:0)

C中的for循环的工作原理如下:

for ( variable initialization; condition; variable update ) {
    /* Do something... */
}

只要条件 为真,循环就会执行。所以,当你这样做时:

for (i = 1; i == count; ++i)

只要 i == count 为真,循环就会执行。因此,除非 count 在执行此行时保持 1 ,否则循环将永远不会运行。

正如其他人指出的那样,你可能想要这个:

for (i = 1; i <= count; ++i)

因此,您的循环将针对 i 的所有值运行,直到达到计数

作为旁注,我应该指出在C中编写for循环的通常方法是这样的:

for (i = 0; i < count; i++)

我们从 i = 0 开始,因为C数组是从零开始的,所以数组的第N个元素的索引 n-1

答案 4 :(得分:0)

你太近了。除了修复循环测试条款for (i = 1; i <= count; i++)之外,我建议您使用" %d"作为格式说明符。您的do循环只需要while循环,以避免每次都打印invalid number消息。

此外,虽然不是错误,但C的标准编码样式避免使用caMelCase变量来支持所有小写。参见例如NASA - C Style Guide, 1994

通过这些更改,(并将奇数/偶数检查更改为简单的&),您可以按如下方式编写代码。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
// #define PAUSE system("Pause")

int main (void)
{
    int number, i, odd, even, totalnum, tempnum, count;
    number = i = odd = even = totalnum = tempnum = count = 0;

    printf ("enter a number between 2 and 25: ");
    scanf (" %d", &number);

    while (number < 2 || number > 25) {
        printf ("invalid number, again (between 2 and 25): ");
        scanf (" %d", &number);
    }

    printf ("numbers to input: ");
    scanf (" %d", &count);

    for (i = 1; i <= count; i++) {
        printf ("input number %2d: ", i);
        scanf (" %d", &tempnum);
        if ((tempnum & 1) == 0)
            even++;
        else
            odd++;
        totalnum = totalnum + tempnum;
    }

    printf ("You entered %d numbers\n", count);
    printf ("The sum of the %d numbers is %d\n",
            count, totalnum);
    printf ("The average of the %d numbers is %d\n",
            count, totalnum / count);
    printf ("You entered %d odd numbers and %d even numbers\n",
            odd, even);

//     PAUSE;
    return 0;
}

注意: mainint类型(例如int main (int argc, char **argv)或简称int main (void),表示没有参数)。由于它是int类型,它将return一个shell的值。虽然历史实现可能允许void main不再适用于可移植代码。

示例使用/输出

$ /bin/forskipped
enter a number between 2 and 25: 4
numbers to input: 4
input number  1: 1
input number  2: 2
input number  3: 3
input number  4: 4
You entered 4 numbers
The sum of the 4 numbers is 10
The average of the 4 numbers is 2
You entered 2 odd numbers and 2 even numbers

仔细看看,如果您有任何问题,请告诉我。