如何在 Scanner 的 armstrongnumber 计算中克服 misMatchExeption?

How do i overgo a misMatchExeption in an armstrongnumber calculation in Scanner?

我写了下面的代码,条件如下。我不允许使用任何字符串或数学 class,所以我使用 for 循环来分解数字。

它适用于例如 153 和 54883,但对于像 4679307774 这样的数字,扫描仪类型返回 misMatchExeption。我明白为什么,我尝试使用 long 类型,然后程序运行,但没有(由于 Two`s)返回正确答案。

我想知道,如何解决这个问题,或者最好说一下我可以在这里尝试的其他事情。

Scanner sc = new Scanner(System.in);

    System.out.println("Please enter any Integer above zero here : ");
    System.out.println("Enter length of number, from 1 onwards: ");

    int num = sc.nextInt();
    int pow = sc.nextInt();

    int narziss = 0;  // TODO mismatchexeption
    int single;
    int a;

    do {
        a = 1;
        single = num % 10; // takes each chiffre from behind, for as long as for runs.
        System.out.println("single modulo : " + single);
        num = num / 10;

        for (int i = 0; i < pow; i++) {

            a *= single;
            System.out.println(a);
        }
        narziss += a;
        System.out.println("narziss: " + narziss);

    } while (num != 0);

    System.out.println(" if the last shown number, " +
            "is the same as you have typed in, " +
            "you found an so-called armstrong number! ");
    }
}

我问的问题可以用 Integer 以外的类型解决。 例如长...或浮点类型。

务必更换或铸造所有相关部件,如扫描仪、环路等。