I'm trying to make this code, which is basically a 6x6 two-dimensional array with the distance between 6 cities, then collect the route the user made in a simple array [6], and calculate the total miles that he traveled.
The code:
package rotas;
import java.util.*;
public class Rotas {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] km = {{0, 20, 32, 45, 85, 90}, {20, 0, 20, 40, 65, 70}, {32, 20, 0, 25, 48, 49},
{45, 40, 25, 0, 39, 52}, {85, 65, 48, 39, 0, 36}, {90, 70, 49, 52, 36, 0}};
int[] rota = new int[6];
String[] rota_r = new String[6];
String[] cid = {"Belo Horizonte", "Contagem", "Betim", "Juatuba", "Pará de Minas", "Itaúna"};
int km_rodados = 0;
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
System.out.printf("%d\t", km[i][j]);
}
System.out.printf("\n");
}
System.out.println("Digite sua rota: \n\n1. Belo Horizonte\t2. Contagem\t3. Betim\n"
+ "4. Juatuba\t5. Pará de Minas\t 6.Itaúna\n");
for(int i = 0; i < 6; i++){
rota[i] = input.nextInt();
}
System.out.println("\nROTA: \n");
for(int i = 0; i < 6; i++){
System.out.printf("%d. %s\n", i+1, cid[rota[i]-1]);
}
for(int i = 0; i <= 6; i++){
km_rodados = km_rodados + km[rota[i]][rota[i+1]];
}
System.out.printf("\nKilômetros rodados: %d\n", km_rodados);
}
}
Until the part of collecting the route and displaying them in beauty order, but at the time of calculating the km, it goes to 5. Give a help ai, I could not think of another way to calculate this.