在用户给定的间隔之间找不到阿姆斯壮编号的程序出错

时间:2019-03-29 09:40:35

标签: c++

错误:

  

// Count(变量)未声明!错误。

但是我已经宣布了。这是一个计算用户给定间隔内的阿姆斯壮位数的程序。它将继续运行,直到间隔中至少有一个阿姆斯壮编号。我为此使用了Do-While循环。

C ++:

#include<iostream>
#include<cmath>  

using namespace std;

//Function to check if the number is an Armstrong number
bool Armstrong(int n) {
    int number, original, remainder, result = 0, k = 0;
    original = n;
    //Calculating the number of digits
    while (original != 0) {
        original /= 10;
        ++k;
    }

    original = n;

    while (original != 0) {
        remainder = original % 10;
        result += pow(remainder, k);
        original /= 10;
    }

    if (result == n)
        return true;
    else
        return false;
}

//Checking Armstrong in the interval
int main() {
    do {
        int start, stop, n, i = 1;
        std::cout << "Enter Starting Point: ";
        std::cin >> start;
        std::cout << "Enter Stop Point: ";
        std::cin >> stop;
        n = start;
        int count = 0; //printing the numbers in the interval
        for (; start <= stop; start++) {
            if (Armstrong(start)) {
                std::cout << "Armstrong Number " << i << " : " << start;
                count++;
                i++;
            }
            n--;
        }
        //It is showing the error here. "Count not Declared"
    }
    while (count == 0);
}

1 个答案:

答案 0 :(得分:2)

问题是您在java.lang.NoSuchMethodException: android.os.MessageQueue#enableMonitor()#bestmatch循环内声明了int count;,因此无法在循环条件下检查它。将其移到循环之外:

do-while