Я использую Eclipse, и когда я отлаживаю и просматриваю код, мой внешний цикл не увеличивается, а корень всегда остается равным 2. Может ли кто-нибудь сказать мне, почему? Верхний комментарий объясняет, что я пытаюсь выполнить.Может ли кто-нибудь сказать мне, почему мой внешний цикл for не увеличивает эту переменную?
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