java - Recursive Palindrome -
first of not telling "do homework." need little help on how maintain repeating process. programme did below , has tester class it.
the class:
class recursivepalindrome { public static boolean ispal(string s) { if(s.length() == 0 || s.length() == 1) homecoming true; if(s.charat(0) == s.charat(s.length()-1)) homecoming ispal(s.substring(1, s.length()-1)); homecoming false; } }
then tester class has main method:
public class recursivepalindrometester { public static void main(string[] args) { recursivepalindrome pal = new recursivepalindrome (); boolean quit = true; scanner in = new scanner(system.in); system.out.print("enter word test whether palindrome or not(press quit end.): "); string x = in.nextline(); while(quit) { boolean itspal = pal.ispal(x); if(itspal == true){ system.out.println(x + " palindrome."); quit = false; } else if (x.equals("quit")) { quit = false; } else { quit = false; system.out.println(x + " not palindrome."); } } } }
this programme find if letter palindrome or not. got calculations , stuff in do maintain asking user input , every time user inputs says if palindrome word or not.
simply wrap while loop.
look continue , break statements. helpful loops, you're looking info on here. public class recursivepalindrometester {
public static void main(string[] args) { recursivepalindrome pal = new recursivepalindrome (); scanner in = new scanner(system.in); while(true){ system.out.print("enter word test whether palindrome or not(press quit end.): "); string x = in.nextline(); boolean itspal = pal.ispal(x); if(itspal == true){ system.out.println(x + " palindrome."); } else if (x.equals("quit")) { break; } else { system.out.println(x + " not palindrome."); } } } }
java
No comments:
Post a Comment