防雷工程,根據Armstrong numbers定義:
package math.numbers;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Created by Michael
* Creation date 7/28/2017.
* @link https://stackoverflow.com/questions/45378317/confused-with-int-assignments-in-java
* @link http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html
*/
public class Armstrong {
public static void main(String[] args) {
List<Integer> armstrong = IntStream.range(0, 1000000).filter(Armstrong::isArmstrong).boxed().collect(Collectors.toList());
System.out.println(armstrong);
}
public static boolean isArmstrong(int n) {
boolean armstrong = false;
int sum = 0;
int temp = n;
while (temp > 0) {
int x = temp % 10;
sum += x * x * x;
temp /= 10;
}
armstrong = (sum == n);
return armstrong;
}
}
這裏的輸出我得到:
[0, 1, 153, 370, 371, 407]
您將能夠告訴更快,如果你跑了在調試器中編寫代碼,設置一個斷點,然後逐步瞭解您的假設是否不正確。 – duffymo
最後,num = 0.嘗試在循環之前使用'temp2 = num'將'num'的值存儲到某個'temp2'變量中,最後做'System.out.println(temp2 == armNum); ' –