簡體   English   中英

為什么在“ for”循環中收到一條錯誤消息,說“ break”語句不在循環或切換中?

[英]Why am I receiving an error at my “for” loop saying 'break' statement not in loop or switch?

編程上的新手令人難以置信,我在第12行收到一條錯誤消息,指出我的break語句不在循環或切換中。 誰能解釋我的錯誤在哪里以及如何解決?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int n1, n2, i, gcd, lcm;

    printf("Enter two positive integers: ");
    scanf("%d %d",&n1,&n2);

    for(i=1; i <= n1 && i <= n2; ++i) {
        printf("Enter two positive integers: ");
        scanf("%d %d",&n1,&n2);
        if(n1==-1,n2==-1) break;
        // Checks if i is factor of both integers
        if(n1%i==0 && n2%i==0)
           gcd = i;
    }

    lcm = (n1*n2)/gcd;
    printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);

    return 0;
}

如果您“在第12行收到一條錯誤消息,指出我的break語句不在循環或切換中”,則說明您的編譯器功能很不足或發布了錯誤的代碼。 該代碼有一些問題,但在錯誤的地方break並不是其中的一個。

當您將大括號放錯位置時,通常會發生以下特定錯誤消息:

int i;
for (i = 0; i < 10; ++i)
    printf("%d\n", i);
    if (someCondition)
        break;

這是因為,盡管它看起來像你打破了一個循環的事實,實際break語句不是在循環 只有printf是。

在您提供的代碼而言,有許多方法可以清理:

  • 刪除不需要的包含。
  • 將數字輸入重構為通用功能。
  • 允許一個非正數終止程序。
  • 使用DRY原理進行輸入,如果正確構建代碼段,實際上確實需要復制代碼段。
  • 使輸入更可靠,允許輸入無效數字。
  • 添加更多注釋,這些注釋將在以后為您(或其他必須維護您的代碼的人)提供極大的幫助。
  • 使用更好的變量名。 除了用於局部小循環的i ,我幾乎從不使用單字符變量名。
  • 修復if(n1==-1,n2==-1)位。 那並沒有您所想的那樣。 逗號運算符將計算兩個表達式,但完整表達式的結果是最右邊的一個。 所以if(n2==-1)是有效的。

為此,以下是編寫代碼的方式:

#include <stdio.h>

#define ERR_NON_POS -1
#define ERR_INVALID -2

// Gets a single number.
//  If non-positive or invalid, returns error (a negative value ERR_*).
//  Otherwise, returns the (positive) number.

int getNumber(void) {
    int number;
    if (scanf("%d", &number) != 1) return -2;
    if (number <= 0) return -1;
    return number;
}

int main(void) {
    // Infinite loop, we'll break from within as needed.

    for (;;) {
        printf("Enter two positive integers (a negative number will stop): ");

        // Do it one at a time so a SINGLE negative number can stop.

        int number1 = getNumber();
        if (number1 == ERR_INVALID) {
            puts("** Non-integral value entered");
            break;
        }
        if (number1 == ERR_NON_POS) break;

        int number2 = getNumber();
        if (number2 == ERR_INVALID) {
            puts("** Non-integral value entered");
            break;
        }
        if (number2 == ERR_NON_POS) break;

        // Work out greatest common divisor (though there are better ways
        //  to do this than checking EVERY possibility).

        int gcd = 1;
        for (int i = 2; (i <= number1) && (i <= number2); ++i) {
            if (number1 % i == 0 && number2 % i == 0) {
                gcd = i;
            }
        }

        // Work out the lowest common multiple.

        int lcm = number1 * number2 / gcd;

        // Print them both and go get more.

        printf("For numbers %d and %d, GCD is %d and LCM is %d.\n", number1, number2, gcd, lcm);
    }

    return 0;
}

而且,如果您想知道更有效的GCD計算方法,則應該研究Euclid的算法。 可以定義為(對於非負ab ):

gcd(a,b) = a               if b is zero
           gcd(b, a mod b) if b is non-zero

這意味着您可以具有遞歸函數:

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

如果您強烈反對遞歸,則可以使用迭代方法:

int gcd(int a, int b) {
    while (b != 0) {
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}

暫無
暫無

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

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