2017-02-22 17 views
-1

我使用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); 
    } 
} 
+2

聲明並初始化在各自的循環迴路變量。 – Jyr

+0

您可以在循環中添加打印語句以查看變量的值。在你的內部循環中添加System.out.println(「root:」+ root +「expo:」+ expo);它將打印這些變量在循環執行時所採用的值。 – Ali

回答

1

你是不正確的。 root變量每次都增加,但我猜你每次外層循環迭代時都忘記初始化變量expo

想一想,當外循環完成其第一次迭代時(對於root = 2),expo的值將變爲21,並且由於您沒有將其重新初始化爲2,所以內循環未執行剩餘的迭代次數外環。

爲了更好地理解execture下面的代碼片段,看看會發生什麼!

for (root = 2; root <= 20; root++) { 
    System.out.print(root + " --> "); 
    for (expo = 2; expo <= 20; expo++) { 
     // your code goes here 
     System.out.print(expo + " "); 
    } 
    System.out.println(""); 
} 

它輸出:

2.0 --> 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0 
3.0 --> 
4.0 --> 
5.0 --> 
6.0 --> 
7.0 --> 
8.0 --> 
9.0 --> 
10.0 --> 
11.0 --> 
12.0 --> 
13.0 --> 
14.0 --> 
15.0 --> 
16.0 --> 
17.0 --> 
18.0 --> 
19.0 --> 
20.0 --> 

解決你的問題,你可以作如下更新你的代碼。

for (root = 2; root <= 20; root++) { 
    for (expo = 2; 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); 
    } 
} 

請參閱Live Demo你的代碼。

+0

我做了你所說的並沒有改變任何東西。我得到不同的輸出,但是當我調試它仍然顯示根不增加停留2.另外在原始代碼,當我調試世博會的價值不成爲21,所以我不知道你的意思是 –

+0

@ A.PROSCIA看現場演示你的代碼在這裏 - https://ideone.com/9Dz4q3。如果你仍然不知道問題是什麼,那麼我就戒菸了。 –

1

您需要爲外循環的每次運行重新初始化expo變量。

這本

double root, expo, prodX, prodY, leySum1 = 0, leySum2 = 0; 
     for (root=2; root <= 20; root++) { 
      for (expo=2; 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); 
相關問題