无法在循环过程中更改变量

时间:2012-03-04 17:28:39

标签: c variables loops

因此,分配是要求在某个范围内的变量打印数字的各个数字,数字之间有三个空格。例如1234应打印

0 1 2 3 4
1 2 3 4
2 3 4
3 4 
4

我很确定我完成了大部分任务,但我在循环中更改变量号时遇到了麻烦。该数字将自己排序到右边的if语句中,但是当它循环而不是数字下移一个数字(即2343到343)时,它所做的就是打印相同的数字5次。我已经在我的书中研究并在线查看,但我没有看到它。它可能很简单,只是不确定是什么。这是代码:

#include <stdio.h>
#include <stdlib.h>
void loopingDigitprinter(int digit);
int division(int* digit);

int main() 
{
    int digitPrint;



    printf("Please enter a number between 0 and 32,767: ");
    scanf("%d", &digitPrint);
    loopingDigitprinter (digitPrint);

    return 0;
}

void loopingDigitprinter(int digit)
{
    int loopLine= 0;
   int thousand;
   int hundred;
   int original;
   original = digit;



    while(loopLine < 4)
    {
        if (digit > 10000 && digit <= 32767)
            {
                thousand = digit/ 1000;
                hundred = digit % 1000;
                printf("%02d%03d\n",thousand, hundred);
                digit %= 10000;


            }
        else if (digit < 10000 && digit > 1000)
            {

              if (original > 10000)
                    {   thousand = digit/ 1000;
                        hundred = digit % 1000;
                        digit %= 1000;
                        printf("%01d%03d\n",thousand, hundred);
                    }
            else
                    {
                        thousand = digit/ 1000;
                        hundred = digit % 1000;
                        printf("%02d%03d\n",thousand, hundred);
                        digit %= 1000;
                    }

            }
         else if (digit < 1000 && digit > 100)
            {
                if (original > 10000)
                    {
                         hundred = digit % 1000;
                         printf("%d\n",  hundred);
                         digit %= 100;
                    }  
                 else if (original < 10000 && original > 1000)
                    {
                        thousand = original / 1000;
                        hundred = digit % 1000;
                        printf("%d%d\n",thousand,digit);
                        printf("%d\n", digit);
                        digit %= 100;
                        digit %= 100;

                    }
                else
                    {
                         thousand = digit/ 1000;
                        hundred = digit % 1000;
                         printf("%02d%03d\n",thousand, hundred);
                        digit %= 1000;
                         thousand = original / 1000;
                         hundred = digit % 1000;
                         printf("%d%d\n",thousand,digit);
                         printf("%d\n", digit);
                         digit %= 100;
                         printf("%d\n", digit);
                         digit %= 10;
                         printf("%d\n", digit);
                     }

             }
         else if (digit < 100 && digit > 10)
            {
                if (original > 10000)
                     {   hundred = digit % 1000;
                         printf("%d\n", hundred);
                         digit %= 10;
                         printf("%d\n", digit);
                     }
                 else if (original < 10000 && original > 1000)
                     {
                         thousand = original / 1000;
                         hundred = digit % 1000;
                         printf("%d\n",hundred);
                         digit %= 10;
                        printf("%d\n", digit);
                    }
                 else if (original < 1000 && original > 10)
                    {
                        thousand = digit/ 1000;
                        hundred = digit % 1000;
                        printf("%02d%03d\n",thousand, hundred);
                        digit %= 1000;
                        thousand = original / 1000;
                        hundred = digit % 1000;
                        printf("%d%03d\n",thousand,digit);
                        printf("%03d\n", digit);
                        digit %= 100;
                        printf("%d\n", digit);
                        digit %= 10;
                        printf("%d\n", digit);

                    }
                else
                   printf("1");
               }

        else if(original > 0 && original < 10)

            {
                 printf("0000%d\n", original);
                 printf("000%d\n", original);
                 printf("00%d\n", original);
                 printf("0%d\n", original);
                 printf("%d\n", original);
                break;
            }



         loopLine++;
        }

        return;
 }

2 个答案:

答案 0 :(得分:0)

每个循环都会调用loopLine ++,因此最终会退出,但请仔细检查上次的相等检查。数字不能大于10且小于0.

您可能需要在上一个else if中调用除法(数字)以进行更改。

答案 1 :(得分:0)

我不知道你在那里做什么,因为我没有阅读代码。

但是请参考我的示例代码。它几乎是必要的

#include <stdio.h>
#include <string.h>

int main(int argc, char*argv[]){

int n,k,i=0;

printf("Enter a number please\n");
scanf("%d",&n);

while(i<=n){

    for(k=i;k<=n;k++){

        printf("%d ",k);

    }

    printf("\n");   
    i++;
}

return 0;
}


Output
------
0 1 2 3 4
1 2 3 4
2 3 4
3 4 
4