2017-06-04 112 views
-8
public class A{ 
public static void main(String[] args) { 
    int a; 
    int b;int c=0; 
    for(a=100;a<=999;a++){ 
     for(b=100;b<=999;b++){ 
      int n=a*b; 
      int temp=n; 
      while(n>0){ 
       int r=n%10; 
       c=(c*10)+r; 
       n=n/10; 
      } 
      if(temp==c){ 
       System.out.println(c); 
      } 
     } 
    } 
    } 
} 

代码编译良好,但运行时只是跳过所有内容并退出。请帮助。 P.S.问题4欧拉计划代码跳过运行

+1

你应该学会[如何调试程序(https://ericlippert.com/2014/03/05/how-to-调试小程序/)。 –

回答

1

它不会输出任何东西,因为temp==cfalse

2

首先让格式的代码很容易阅读:

public class A { 
    public static void main(String[] args) { 
     int a; 
     int b; 
     int c = 0; 
     for (a = 100; a <= 999; a++) { 
      for (b = 100; b <= 999; b++) { 
       int n = a * b; 
       int temp = n; 
       while (n > 0) { 
        int r = n % 10; 
        c = (c * 10) + r; 
        n = n/10; 
       } 
       if (temp == c) { 
        System.out.println(c); 
       } 
      } 
     } 
    } 
} 

现在我们注意到,仅几个街区保持我们而去从main方法内部的print语句是两个for循环,以及一个if statemet。 检查两个循环,它们没有明显的错误,所以我们排除它们,剩下的就是if语句。在这一点上,我们可以假设temp永远不等于c。 如果你不能通过查看代码来跟踪为什么这个conition不满意,你可以使用print语句做一些简单的调试(即在if之前打印变量c和temp,例如直观地检查它们的值),或者使用一些例如,您可以在IDE上找到更高级的调试工具。

指南在调试运行:

How to debug a Java program without using an IDE?

http://www.vogella.com/tutorials/EclipseDebugging/article.html