下面是我的代码。
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 数字。
...是一个数字,它是它自己的数字之和,每个数字都增加到数字的幂次方。
慕无忌1623718
aluckdog
相关分类