我使用Eclipse,當我調試並通過代碼時,我的外部循環不會增加,並且根始終保持爲2.任何人都可以告訴我爲什麼?最重要的評論解釋了我試圖完成的事情。任何人都可以告訴我爲什麼我的外循環不增加變量?
public class First_120_Numbers {
/*A Leyland number sequence is a series of numbers of the formxy + yxwhere x and y are integers greater than 1.
* They are named after the mathematician Paul Leyland. The first few Leyland numbers are
*8, 17, 32, 54, 57, 100, 145, 177, 320, 368, …
*Take 145 as an example where it is x^y+y^x = 34+43
*By considering the terms in the Leyland sequence find the sum of the first 20 numbers in the Leyland sequence.*/
public static void main(String[] args) {
double root, expo, prodX, prodY, leySum1 = 0, leySum2 = 0;
root = 2; // Leyland numbers must be greater than 1
expo = 2;
for (; root <= 20; root++) {
for (; expo <= 20; expo++) {
prodX = Math.pow(root, expo); //raises root to expo
prodY = Math.pow(expo, root);// raises expo to root
leySum1 = prodX + prodY;
leySum2 += leySum1;
System.out.println(leySum1);
}
}
System.out.println("The sum of the leyland numbers "
+ "up to 20 is " + leySum2);
}
}
聲明並初始化在各自的循環迴路變量。 – Jyr
您可以在循環中添加打印語句以查看變量的值。在你的內部循環中添加System.out.println(「root:」+ root +「expo:」+ expo);它將打印這些變量在循環執行時所採用的值。 – Ali