猿问

找出数字之间的阿姆斯特朗数

下面是我的代码。


package com.ofss.java.examples;


import java.util.Scanner;


class ArmstrongNumber {

    public static void main(String[] args) {

        int c = 0, a;

        int n1, n2;//Range in which armstrong number need to find


        Scanner s = new Scanner(System.in);

        System.out.println("Enter the first number");

        n1 = s.nextInt();

        System.out.println("Enter the second number");

        n2 = s.nextInt();


        for (int i = n1; i <= n2; ++i) {

            while (i > 0) {

                a = i % 10;

                System.out.println(a);

                i = i / 10;

                System.out.println(i);

                c = c + (a * a * a);

                System.out.println(c);

            }

            if (i == c)

                System.out.println(c + "armstrong number");

            else

                System.out.println(c + "Not armstrong number");


        }

    }

}

执行后我得到不正确的结果。代码无限运行,直到你停止它。它必须打印 151-154 之间的数字(armstrong 为 153)。

此外,它错误地将 153 打印为不是 Armstrong 数字。

...是一个数字,它是它自己的数字之和,每个数字都增加到数字的幂次方。


慕少森
浏览 184回答 3
3回答

慕无忌1623718

您不应该更改,i因为这也用于for (int i = n1; i <= n2; ++i)&nbsp;或者您可能永远不会退出该循环,因为您希望i在第一次迭代结束时为负。很难增加直到达到n2。使用不同的变量来i安全地跟踪。&nbsp; &nbsp; int j = i;&nbsp; &nbsp; while(j > 0) ...关于阿姆斯壮数:阿姆斯特朗数是一个数,它是它自己的数字之和,每个数字都增加到数字的幂次方您需要将每个数字的数字长度(数字的数量)的幂。153 = 1^3 + 5^3 + 3^31634 = 1^4 + 6^4 + 3^4 + 4^4这是它的方法:public static boolean isArmstrongNumber(int number){&nbsp; &nbsp; int power = Integer.toString(number).length(); //just to get the number of digit...&nbsp; &nbsp; int tmp = number;&nbsp; &nbsp; int digit , sum = 0;&nbsp; &nbsp; while(tmp > 0){&nbsp; &nbsp; &nbsp; &nbsp; digit = tmp % 10;&nbsp; &nbsp; &nbsp; &nbsp; sum += Math.round(Math.pow(digit , power));&nbsp; &nbsp; &nbsp; &nbsp; tmp /= 10;&nbsp; &nbsp; }&nbsp; &nbsp; return sum == number;}使用这个从 0 到 10.000 的检查给出:0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474与维基百科相同:自恋数字的基数为 10 的序列开始:0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, ...请注意,使用方法可以消除c在您的情况下忘记重置变量的风险。纠正这个会给你更多“正确”的结果(以及 3 位数的结果)您还可以使用 less 数学来读取数字并使用char[],请记住,您需要减去'0'value 才能获得字符的数值:public static boolean isArmstrongNumber(int number){&nbsp; &nbsp; char[] digits = Integer.toString(number).toCharArray();&nbsp; &nbsp; int power = digits.length;&nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; for(char c : digits){&nbsp; &nbsp; &nbsp; &nbsp; int digit = c - '0';&nbsp; &nbsp; &nbsp; &nbsp; sum += Math.round(Math.pow(digit, power));&nbsp; &nbsp; }&nbsp; &nbsp; return sum == number;}

aluckdog

公共类 ArmstrongNumber {private final int n1, n2;public ArmstrongNumber(int n1, int n2) {&nbsp; &nbsp; this.n1 = n1;&nbsp; &nbsp; this.n2 = n2;}protected static boolean isArmstrong(int n) {&nbsp; &nbsp; if(n < 0)&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; int remaining=n;&nbsp; &nbsp; int sumCube=0;&nbsp; &nbsp; while (remaining>0) {&nbsp; &nbsp; &nbsp; &nbsp; int d = remaining % 10;&nbsp; &nbsp; &nbsp; &nbsp; sumCube += cube(d);&nbsp; &nbsp; &nbsp; &nbsp; remaining /= 10;&nbsp; &nbsp; }&nbsp; &nbsp; return n == sumCube;}private static int cube(int d) {&nbsp; &nbsp; return d*d*d;}public Integer[] find() {&nbsp; &nbsp; List<Integer> results = new ArrayList<>();&nbsp; &nbsp; for (int i = n1; i <= n2; ++i)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (isArmstrong(i))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.add(i);&nbsp; &nbsp; }&nbsp; &nbsp; return results.toArray(new Integer[0]);}}
随时随地看视频慕课网APP

相关分类

Java
我要回答