简体   繁体   中英

Why won't it print anything?

This is the code I have written. But the code does not print anything even though it is compiling. I have also tried to include System.out.print statements under the if and else statements. What am I supposed to do in order to have it actually print something.

public class Numfive {
    public static void main(String[] args) {
        isReverse("hello", "eLLoH");
    }

    public static boolean isReverse(String s1, String s2) {
        if (s1.length() == 0 && s2.length() == 0) { 
            return true;
        } else if (s1.length() == 0 || s2.length() == 0) {
            return false;  // not same length
        } else {
            String s1first = s1.substring(0, 1);
            String s2last = s2.substring(s2.length() - 1);
            return s1first.equalsIgnoreCase(s2last) &&
            isReverse(s1.substring(1), s2.substring(0, s2.length() - 1));
        }
    }
}

Because you don't have any print statement (that prints the result).

System.out.println(isReverse("hello", "eLLoH"));

Note:

I have also tried to include System.out.print statements under the if and else statements.

If you put a print statement after the if-else if-else structure, the program won't never reach it, because each block has a return .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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