简体   繁体   English

System.out.println不打印输出?

[英]System.out.println is not printing output?

I am trying to have the list for palindrome but it doesn't give any kind of output. 我正在尝试列出回文清单,但没有给出任何输出。 But it works when I am checking for palindrome. 但是,当我检查回文时,它可以工作。

public class Main {
    public static void main(String[] args) {
        int num=0 ,org=0 , rem = 0,rev=0 ,a=0;
        for(num=12 ; num<=101; num++) {
            org=num ;

            while(num>0) {
                rem = num % 10 ; 
                num = num /10 ;
                rev= (10*rev)+rem;
            }

            if(rev==org)
                System.out.println(org);
        }
    }
}

Why am I not getting any output? 为什么我没有得到任何输出?

You have two errors : 您有两个错误:

  1. You should reset rev to 0 in each iteration (otherwise, rev would only be correct in the first iteration). 您应该在每次迭代中将rev重置为0(否则, rev仅在第一次迭代中是正确的)。
  2. You should add num=org; 您应该添加num=org; at the end of each iteration, to restore the value of your loop variable. 在每次迭代结束时,恢复循环变量的值。 Otherwise, the loop may never terminate, since you are messing with the loop variable inside the body of the loop. 否则,循环可能永远不会终止,因为您正在弄乱循环体内的循环变量。

It should look like this : 它应该看起来像这样:

for(num=12 ; num<=101; num++) {   
    org=num ;   
    rev = 0; // added
    while(num>0) {
      rem = num % 10 ; 
      num = num /10 ;
      rev= (10*rev)+rem;
    }
    if(rev==org)
        System.out.println(org);
    num = org; // added
}

Output : 输出:

22
33
44
55
66
77
88
99
101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM