Advertisement
Guest User

doWhileOuter

a guest
Oct 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1.     public static void main(String[] args) {
  2.         // asks the user for two words then check to see if the two strings are equal.
  3.         // assuming the two words are different print them out in the correct alphabetic
  4.         // order
  5.         // repeats if user says no
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         outer: do {
  10.             System.out.println("Please enter TWO words");
  11.  
  12.             String wordOne = scan.nextLine();
  13.             String wordTwo = scan.nextLine();
  14.             int compare = wordOne.compareToIgnoreCase(wordTwo);
  15.  
  16.             if (!wordOne.equalsIgnoreCase(wordTwo)) {
  17.                 System.out.println("They are not equal!");
  18.                 if (compare < 0) {
  19.                     System.out.println(wordOne + " comes before " + wordTwo);
  20.                 } else if (compare > 0) {
  21.                     System.out.println(wordTwo + " comes before " + wordOne);
  22.                 }
  23.             } else {
  24.                 System.out.println("They are the same...");
  25.             }
  26.  
  27.             do {
  28.                 System.out.println("Do you want to try again? y/n");
  29.                 String cont = scan.nextLine();
  30.  
  31.                 if (cont.equalsIgnoreCase("n")) {
  32.                     System.out.println("Thank you for using the program!");
  33.                     break outer;
  34.                 } else if (cont.equalsIgnoreCase("y")) {
  35.                     break;
  36.                 } else {
  37.                     System.out.println("INVALID, please enter y/n");
  38.                 }
  39.             } while (true);
  40.         } while (true);
  41.  
  42.         System.out.println("The End");
  43.         scan.close();
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement