Created
May 12, 2015 16:07
-
-
Save MrBean83/eed68427ba38ae7dc9c2 to your computer and use it in GitHub Desktop.
Prints out string if string length is even; prints out reversed copy of string if string length is odd.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
public class StringOddEven { | |
public static void main(String args[]) { | |
String input = JOptionPane.showInputDialog("Please enter a string."); | |
int length = input.length(); | |
if (length % 2 == 0) { | |
System.out.println(input); | |
} else { | |
int i = length - 1; | |
// Using a DO-WHILE loop for this program. | |
do { | |
System.out.print(input.charAt(i)); | |
i--; | |
} while (i >= 0); // end do-while | |
} // end if | |
} // end main | |
} // end class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment