What do I need to add to this code to say \"Thanks for using my program\" after
ID: 3585201 • Letter: W
Question
What do I need to add to this code to say "Thanks for using my program" after the user enters "done" and the program is ended
import java.util.Scanner;
public class ReverseStringRecursive
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string to reverse, or 'done' to exit: ");
String s = scan.next();
while (!s.equalsIgnoreCase("done"))
{
System.out.println("Reverse String: "+reverseString(s));
System.out.println("Enter the string to reverse, or 'done' to exit: ");
s=scan.next();
}
}
public static String reverseString(String s)
{
if(s.length() == 1)
{
return s;
}
else
{
return s.charAt(s.length()-1)+reverseString(s.substring(0,s.length()-1));
}
}
}
Explanation / Answer
ReverseStringRecursive.java
import java.util.Scanner;
public class ReverseStringRecursive {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string to reverse, or ‘done’ to exit: ");
String s = scan.next();
while(!s.equalsIgnoreCase("done")) {
System.out.println("Reverse String: "+reverseString(s));
System.out.println("Enter the string to reverse, or ‘done’ to exit: ");
s = scan.next();
}
System.out.println("Thanks for using my program");
}
public static String reverseString(String s) {
if(s.length() == 1) {
return s;
} else {
return s.charAt(s.length()-1)+reverseString(s.substring(0,s.length()-1));
}
}
}
Output:
Enter the string to reverse, or ‘done’ to exit:
abcd
Reverse String: dcba
Enter the string to reverse, or ‘done’ to exit:
Finally
Reverse String: yllaniF
Enter the string to reverse, or ‘done’ to exit:
done
Thanks for using my program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.