为什么我的循环没有运行

时间:2015-02-04 16:26:09

标签: c

我通过循环字母和数字来测试运行值。但是,由于某种原因,只运行数字循环。

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

int inuti(double x, double y, double x1, double y1, double x2, double y2) {
    int x_inuti;
    int y_inuti;

    if (x1 < x2)
        x_inuti = x > x1 && x < x2;
    else
        x_inuti = x > x2 && x < x1;
    if (y1 < y2)
        y_inuti = y > y1 && y < y2;
    else
        y_inuti = y > y2 && y < y1;
    return x_inuti && y_inuti;
}

int main(void) {
    double x, y, x1, y1, x2, y2;
    char text_x, text_y, text_x1, text_y1, text_x2, text_y2;
    int resultat;
    int i;



    char tecken[] = "abcdefgABCDEFG.+-";
    char teckenback[] = "-+.GFEDXBAgfedcba";
    int max;
    int maxback;
    max = strlen(tecken);
    maxback = strlen(teckenback);

    //These loops do only run if the number loops are commented.

    for (i = 0; i < max; i++) {
        for (i = 0; i < maxback; i++) {
            for (i = 0; i < max; i++) {
                for (i = 0; i < maxback; i++) {
                    for (i = 0; i < max; i++) {
                        for (i = 0; i < maxback; i++) {




                            text_x = tecken[i];
                            text_y = teckenback[i];
                            text_x1 = tecken[i];
                            text_y1 = teckenback[i];
                            text_x2 = tecken[i];
                            text_y2 = teckenback[i];


                            printf("punktens x-varde: %c \n", text_x);


                            printf("punktens y-varde: %c \n", text_y);

                            printf("\n");

                            printf("Ena hornets x-varde: %c \n", text_x1);


                            printf("Ena hornets y-varde: %c \n", text_y1);

                            printf("\n");

                            printf("Andra hornets x-varde %c \n", text_x2);


                            printf("Andra hornets y-varde %c \n", text_y2);

                            printf("\n");

                            printf("##### Endast nummer tillatet! #####");
                            printf("\n");
                            printf("\n");

                        }
                    }
                }
            }
        }
    }




    for (x = 0; x <= 3; x++) {
            for (y = 0; y <= 3; y++) {
                for (x1 = 0; x1 <= 3; x1++) {
                    for (y1 = 0; y1 <= 3; y1++) {
                        for (x2 = 0; x2 <= 3; x2++) {
                            for (y2 = 0; y2 <= 3; y2++){

                                printf("punktens x-varde: %.1f \n", x);


                                printf("punktens y-varde: %.1f \n", y);

                                printf("\n");

                                printf("Ena hornets x-varde: %.1f \n", x1);


                                printf("Ena hornets y-varde: %.1f \n", y1);

                                printf("\n");

                                printf("Andra hornets x-varde %.1f \n", x2);


                                printf("Andra hornets y-varde %.1f \n", y2);

                                printf("\n");

                                resultat = inuti(x, y, x1, y1, x2, y2);


                                if (resultat == 1)
                                    printf("Punkten var inuti rektangeln.\n");
                                else
                                    printf("Punkten var utanfor rektangeln.\n");

                                printf("\n");
                                printf("\n");


                            }
                        }
                    }
                }
            }
        }

        getchar();
    return 0;
}

如果我注释掉数字循环,字母循环正常运行,但是当我运行整个代码时,只运行数字循环。没有错误。有谁知道为什么会这样? 谢谢

1 个答案:

答案 0 :(得分:0)

在第一个循环部分中,如果maxback高于max,则只有最后一个循环可用。在另一种情况下,如果maxback从最大值再次低于1,则它将结束,因为在最后一次迭代之后,您已经到达上述循环的末尾。在任何其他情况下,最后两个循环将无限期地运行。

简而言之,我的回答是:因为第一个循环没有完成,第二个循环不会运行。