Java 如何在扫描仪中打印阵列中的数字

Java 如何在扫描仪中打印阵列中的数字,java,Java,当扫描器要求他们从阵列中打印出一个人选择的城市时,我需要帮助来实现它 // Construct a Scanner. Scanner scanner = new Scanner(System.in); // Not a city value. int city = -1; // Forever... while (true) { // Display the prompt. System.out.print("Please enter a destination city

当扫描器要求他们从阵列中打印出一个人选择的城市时,我需要帮助来实现它

// Construct a Scanner.
Scanner scanner = new Scanner(System.in);

// Not a city value.
int city = -1;

// Forever...
while (true) {

    // Display the prompt.
    System.out.print("Please enter a destination city (1-12)" +
        "1.SFO 2.ATL 3.DET 4.LA 5.CHI 6.NJ " +
        "7.LV 8.MIA 9.BOS 10.DAL 11.DC 12.NY: ");

    // check for an integer.
    if (scanner.hasNextInt()) {

        // get the value.
        city = scanner.nextInt();

    } else {

        if (!scanner.hasNext()) {
            // no more to read.
            break;
        }

        // toss the value.
        scanner.next();

        // not an integer.
        continue;
    }

    // check the range.
    if (city >= 1 && city <= 12) {
        break;
    }

    // bad value.
    city = -1;
}

scanner.close();
而且只需要第三列的数字就可以打印出来

enter code import java.util.*;

 public class path {
 public static int vertex = 0;

 public static void main(String[] args){

  String[] cities = new String[] { "SFO", "ATL",
  "DET", "LA", "CHI", "NJ", "LV", "MIA", "BOS",
  "DAL", "DC", "NY" };

  Scanner scanner = new Scanner(System.in); // Construct a
                                        // Scanner.
   int city = -1; // Not a city value.
    while (true) { // Forever...
   // Display the prompt.
    System.out
    .println("Please enter a destination city (1-12): ");
     for (int i = 0; i < cities.length; i++) { // print the city options.
     System.out.println((1 + i) + ". " + cities[i]); // 1 to cities.length
    }
    if (scanner.hasNextInt()) { // check for an integer.
   city = scanner.nextInt(); // get the value.
   } else {
    scanner.next(); // toss the value.
    continue; // not an integer.
     }
   if (city >= 1 && city <= 12) { // check the range.
    break;
   }  
 city = -1; // bad value.
 }
  scanner.close(); // Close the Scanner.
  if (city != -1) {
  System.out.println("You selected " + cities[city - 1]);
  System.out.println("It will cost you");
  } else {
  System.out.println("No valid city");
  }


   int[][] destination = new int[13][4];
   destination[0][0]= 0;
   destination[1][0]= 1;
   destination[2][0]= 2;
   destination[3][0]= 3;
   destination[4][0]= 4;
   destination[5][0]= 5;
   destination[6][0]= 6;
   destination[7][0]= 7;
   destination[8][0]= 8;
   destination[9][0]= 9;
    destination[10][0]= 10;
    destination[11][0]= 11;
   destination[12][0]= 12;

   for(int i = 0; i < 13; i++){
destination[i][1] = 0;
   }
   destination[0][3] = 0;
    for(int j = 1; j < 13; j++){
destination[j][2] = Integer.MAX_VALUE;
    }
    for(int k = 0; k < 13; k++){
destination[k][3] = 0;
    }

   int[][] edge = new int[13][13];
   edge[0][1] = 50;
    edge[0][2] = 35;
   edge[1][3] = 70;
    edge[1][4] = 60;
    edge[1][5] = 35;
    edge[2][3] = 20;
    edge[2][5] = 60;
     edge[2][6] = 45;
   edge[3][5] = 10;
    edge[4][7] = 30;
    edge[5][7] = 5;
    edge[5][8] = 40;
   edge[6][8] = 30;
    edge[6][9] = 25;
   edge[7][10] = 20;
   edge[8][11] = 50;
   edge[9][12] = 20;
   edge[10][12] = 10;
   edge[11][12] = 15;

   update(destination,edge);
   findV(destination);
     update(destination,edge);
   findV(destination);
   update(destination,edge);
   findV(destination);
   update(destination,edge);
   findV(destination);
   update(destination,edge);
   findV(destination);
   update(destination,edge);
   findV(destination);
   update(destination,edge);
   findV(destination);
   update(destination,edge);
   findV(destination);
    update(destination,edge);
    findV(destination);
    update(destination,edge);
    findV(destination);
    update(destination,edge);
     findV(destination);
     update(destination,edge);
     findV(destination);
      update(destination,edge);
     findV(destination);

     for(int i = 0; i < destination.length; i++)
     {
       for(int j = 0; j < destination[i].length; j++)
        {
       System.out.print(destination[i][j]);
      if(j < destination[i].length - 1) System.out.print(" ");
      }
      System.out.println();

      }
       }

     public static void findV(int[][] destination){
     int min = Integer.MAX_VALUE;
      for(int a = 1; a < 13; a++){
if(destination[a][1] == 0){
    if(destination[a][2] < min){
        min = destination[a][2];
        vertex = a;
    }
        }
     }
destination[vertex][1] = 1;
//update();
     }

    public static void update(int[][] destination, int[][] edge)
        {
for(int x = 0; x < 13; x++){
    if(edge[vertex][x] != 0 && edge[vertex][x] + destination[vertex][2] <    
      destination[x][2] && destination[x][1] == 0){
        destination[x][2] = edge[vertex][x] + destination[vertex][2];
        destination[x][3] = vertex;
    }

            }

        }
         }

我必须说你的代码看起来很熟悉,试试这个

public static void main(String[] args) {
  // First, construct the array of cities,
  String[] cities = new String[] { "SFO", "ATL",
      "DET", "LA", "CHI", "NJ", "LV", "MIA", "BOS",
      "DAL", "DC", "NY" };

  Scanner scanner = new Scanner(System.in); // Construct a
                                            // Scanner.
  int city = -1; // Not a city value.
  while (true) { // Forever...
    // Display the prompt.
    System.out
        .println("Please enter a destination city (1-12): ");
    for (int i = 0; i < cities.length; i++) { // print the city options.
      System.out.println((1 + i) + ". " + cities[i]); // 1 to cities.length
    }
    if (scanner.hasNextInt()) { // check for an integer.
      city = scanner.nextInt(); // get the value.
    } else {
      scanner.next(); // toss the value.
      continue; // not an integer.
    }
    if (city >= 1 && city <= 12) { // check the range.
      break;
    }
    city = -1; // bad value.
  }
  scanner.close(); // Close the Scanner.
  if (city != -1) {
    System.out.println("You selected " + cities[city - 1]);
  } else {
    System.out.println("No valid city");
  }
}

如果我的代码打印出这个值,那么我需要添加什么才能从剩余的数组中返回一个特定的数字呢。请在句首使用正确的标点符号和大写字母,以便清楚地回答您的问题。Elliott Frisch我如何打印出属于城市的号码?@user3101384 System.out.println您选择了城市+城市;当我选择一个城市时,如何打印数组的第三列。第一列数字是我在上面展示的答案中的城市。代码需要将答案打印到用户输入的,以便匹配。因此,如果他们按5,答案应该是65
public static void main(String[] args) {
  // First, construct the array of cities,
  String[] cities = new String[] { "SFO", "ATL",
      "DET", "LA", "CHI", "NJ", "LV", "MIA", "BOS",
      "DAL", "DC", "NY" };

  Scanner scanner = new Scanner(System.in); // Construct a
                                            // Scanner.
  int city = -1; // Not a city value.
  while (true) { // Forever...
    // Display the prompt.
    System.out
        .println("Please enter a destination city (1-12): ");
    for (int i = 0; i < cities.length; i++) { // print the city options.
      System.out.println((1 + i) + ". " + cities[i]); // 1 to cities.length
    }
    if (scanner.hasNextInt()) { // check for an integer.
      city = scanner.nextInt(); // get the value.
    } else {
      scanner.next(); // toss the value.
      continue; // not an integer.
    }
    if (city >= 1 && city <= 12) { // check the range.
      break;
    }
    city = -1; // bad value.
  }
  scanner.close(); // Close the Scanner.
  if (city != -1) {
    System.out.println("You selected " + cities[city - 1]);
  } else {
    System.out.println("No valid city");
  }
}