Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package ejerciciosArraysTema4;
  2. import java.util.Scanner;
  3.  
  4. public class Ejercicio5 {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         String[] vector = {"Madrid", "Barcelona", "Valencia", "Zaragoza"};
  9.                
  10.         String ciudadInicio, ciudadFin;
  11.        
  12.         int x = 0;
  13.         int y = 0;
  14.                
  15.         String[][] distancias =
  16.                        
  17.                                 {{"0", "600", "300", "400"},
  18.                                 {"600", "0", "200", "300"},
  19.                                 {"300", "200", "0", "300"},
  20.                                 {"400", "300", "300", "0"}};
  21.                
  22.          
  23.                
  24.         Scanner lector = new Scanner(System.in);
  25.                
  26.         System.out.println("Introduce el nombre de la ciudad de inicio: ");
  27.         ciudadInicio = lector.next();
  28.         System.out.println("Introduce el nombre de la ciudad de fin: ");
  29.         ciudadFin=lector.next();
  30.        
  31.             for ( int i = 0 ; i  < vector.length ; i++ ) {
  32.                 if ( vector[i].equalsIgnoreCase(ciudadInicio) ) {        
  33.                  x=i;
  34.                 }
  35.                      
  36.             }
  37.             for ( int j = 0 ; j  < vector.length ; j++ ) {
  38.                 if ( vector[j].equalsIgnoreCase(ciudadFin) ) {        
  39.                  y=j;
  40.                }
  41.             }      
  42.             System.out.println("La distancia entre " + ciudadInicio + " y " + ciudadFin + " son " + distancias[x][y] + " kilometros");
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement