Advertisement
Guest User

изначальный код

a guest
Apr 25th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Cities
  6. {
  7. public static String cities[] = {
  8. "Abakan", "Belgorod", "Chelyabinsk", "Dzerzhinsk", "Ekaterinburg",
  9. "Gelendzhik", "Irkutsk", "Krasnoyarsk", "Lipetsk", "Moscow",
  10. "Novgorod", "Omsk", "Petrozavodsk", "Ryazan", "Samara", "Tomsk",
  11. "Ufa", "Volgograd", "Yakutsk", "Zvenigorod"
  12. };
  13.  
  14. public static void main(String[] args) throws IOException
  15. {
  16. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  17. String previousCity = "";
  18. for(;;)
  19. {
  20. System.out.println(previousCity.length() == 0 ?
  21. "Please, type first city:" : "Please, type next city name:");
  22. String city = reader.readLine();
  23. if(previousCity.length() > 0 && isNextCity(previousCity, city)) {
  24. System.out.println("This city is wrong! Try again!");
  25. continue;
  26. }
  27. String nextCity = searchNextCity(city);
  28. System.out.println("My city is: " + nextCity);
  29. previousCity = nextCity;
  30. }
  31. }
  32.  
  33. //=========================================================================
  34.  
  35. private static String searchNextCity(String currentCity)
  36. {
  37. String foundCity = null;
  38. for(String city : cities) {
  39. if(isNextCity(currentCity, city)) {
  40. foundCity = city;
  41. }
  42. }
  43. return foundCity;
  44. }
  45.  
  46. private static boolean isNextCity(String currentCity, String nextCity)
  47. {
  48. int currentCityLastChar = currentCity.length() - 1;
  49. return currentCity.charAt(currentCityLastChar) ==
  50. nextCity.toLowerCase().charAt(0);
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement