From 6782cf507bb9705d139a1feb05b6ddc99a7b9cbf Mon Sep 17 00:00:00 2001 From: Anurag Dash <48097087+dash-anurag@users.noreply.github.com> Date: Thu, 8 Oct 2020 15:48:40 +0530 Subject: [PATCH] Updated the logic The previous logic was wrong. I have replaced it with the correct logic. --- .../Exercise_04_24/Exercise_04_24.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/Exercise_04/Exercise_04_24/Exercise_04_24.java b/Exercise_04/Exercise_04_24/Exercise_04_24.java index 787ad061..cd65248a 100755 --- a/Exercise_04/Exercise_04_24/Exercise_04_24.java +++ b/Exercise_04/Exercise_04_24/Exercise_04_24.java @@ -17,28 +17,26 @@ public static void main(String[] args) { String city3 = input.nextLine(); String temp; - if ((city2.compareTo(city1) < 0) && (city2.compareTo(city3) < 0)) - { - temp = city1; - city1 = city2; - city2 = temp; - } - else if ((city3.compareTo(city1) < 0) && (city3.compareTo(city2) < 0)) - { - temp = city1; - city1 = city3; - city3 = temp; - - } - if (city3.compareTo(city2) < 0) - { - temp = city2; - city2 = city3; - city3 = temp; - } + + if ( city1.compareTo(city2) > 0) { + temp = city1; + city1 = city2; + city2 = temp; + } + if( city1.compareTo(city3) > 0) { + temp = city1; + city1 = city3; + city3 = temp; + } + + if( city2.compareTo(city3) > 0 ) { + temp = city2; + city2 = city3; + city3 = temp; + } // Display cities in ascending order System.out.println("The three cities in alphabetical order are " + city1 + " " + city2 + " " + city3); } -} \ No newline at end of file +}