簡體   English   中英

C 程序的問題(可能通過使用數組來解決)

[英]Issue with C program (maybe solved with use of arrays)

我已經編寫了以下代碼。 但它直到最后的printf才會運行。 另外,如果我設置的驗證未能通過,它會打印出我無法解釋的結果。

#include <stdio.h>

int main(void)
{
    int k, j, z, i, n;
    int l[30];

    // Setting initial values 0 (0=off 1=on)
    for (n=0; n<30; n++)
    {
        l[n] = 0;
    }

    // Employee number
    printf("give employee number\n");
    scanf("%d", &k);

    // Validation of k
    if (k<0 || k>30)
    {
        printf("wrong input");
    }
    else
        // Lamp status change
        for (i=1; i=k; i=i+1)
        {
            for (z=i; z=30; z=2*z)
            {
                if (l[z] = 0)
                   l[z] = 1;
                else
                    l[z] = 0;
            }
        }

        for (j=0; j<30; j++);
        {
            printf("lamp[%d] is: %d\n", j+1, l[j]);
        }

    return(0);
}

我建議你在 C 基礎上多做一些工作......

第一個建議:正如halfer所報告的,您應該注意縮進代碼,它可以讓您(和我們)更輕松地閱讀它。

  • 第一個錯誤,也由 halfer 指出:您可能忘記使用{}聲明代碼塊,提示您的研究知道何時將其放在這里
  • 第二個錯誤:你應該看看for循環語法for (j=0; j<30; j++); 由於;基本上什么都不做在最后。
  • 您混淆了賦值和條件測試, if (l[z]=0)for (i=1; i=k; i=i+1)for (z=i; z=30; z=2*z)沒有條件測試,但賦值(只是=而不是==<=等)所以它們總是正確的......

另外,你沒有解釋你想讓你的代碼做什么......看起來你想打開一些燈,但是帶有錯誤for的雙語句循環令人困惑。 我不知道您是要打開 2^N 燈泡還是只打開用戶選擇的燈泡...這是我對您的代碼的更正:

int main(void) {
    int k, j, z, i, n;
    int l[30];

    // Setting initial values 0 (0=off 1=on)
    for (n=0; n<30; n++) {
        l[n] = 0;
    }

    // Employee number
    printf("give employee number\n");
    scanf("%d", &k);

    // Validation of k
    if (k<0 || k>30) {
        printf("wrong input");
    } else { // New block

        // Lamp status change
        /*
        i = k is an assignment not a test, maybe i == k ?
        but still false, for do while the condition is true
        so use <= and why use i = i+1 here and not i++ like for n++ L6 ?
        Ok for this loop, but with the other you gonna reswitch again and
        again. If you want only switch the one selected, consider to use
        an if instead of the 2nd for loop.
        */
        for (i=1; i <= k; i=i+1) {

            /*
            Same test / assignment misunderstanding.
            personally except 15, I don't know a lot of intergers
            mutiplied by 2 that give 30. Example: if I set 1 in
            my keyboard, k = 1, then i = 1, z = 1, z = 2,z = 4,
            z = 8,z = 16, z = 32, z = 64, etc. to overflow.
            So  z = 30 (ouch z == 30) is never true.
            If you tried to switch only the lamp selected by the user
            I don't see the point of the second for loop.
            But if you wanted to switch 1 light each 2^N bulbs you
            should set z <= 30.
            */
            for (z=i; z<=30; z=2*z) {
                if (l[z] == 0) // = is an assignment, == instead?
                    l[z] = 1; // A block with {} is not needed here because there is only 1 instruction
                else
                    l[z] = 0; // Same as above, {} are not needed here too
            }
        }

        for (j=0; j<30; j++) { // No; here, see the 'for' loop syntax
            printf("lamp[%d] is: %d\n", j+1, l[j]);
        }
    } // End of the else block

    return(0);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM