首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组,如何使用find distance ( Distance,name)函数和显示距离?

数组,如何使用find distance ( Distance,name)函数和显示距离?
EN

Stack Overflow用户
提问于 2017-11-14 18:21:12
回答 1查看 83关注 0票数 0

数组,如何使用find distance ( Distance,name)函数和显示距离?

数组,如何使用find distance ( Distance,name)函数和显示距离?数组,如何使用find distance ( Distance,name)函数和显示距离?

代码语言:javascript
复制
package practice;

public class displayDistance 
{   
   /*I'm playing around with Arrays in Java and had this doubt.
     How do I call the  find Distance (distance,name) and display distance .

     5 Requirement are :
     1) Define 2-D Array for distance and populate it. 
     2) ask user for source and destination ( preferably lets use Exception Handling along with JOptionPane prompt .
        e.g. 1st prompt-> From city :  , 2nd prompt->  To city :    
     3)define a city Array 
     4)call function findDistance (distance,name) to find distance and  return it . 
     5)display distance.*/

    public class practice {
        public void main(String[] args) 
        {

          //1. Define 2-D Array for distance and populate it .      
          int [][] distances = { {0,50,100,20},{50,0,70,110},
                               {100,70,0,200},{20,110,200,0} };

          //3. define a city Array          
          String [] names = {"manassas","fairfax","baltimore","centerville"};       

          // calling (invoking)the function         
           findDistance (distances,names);


          // JOptionPane.showMessageDialog(null, "The distance value is: " + findDistance(distances), "DISTANCE VALUE",
          // JOptionPane.INFORMATION_MESSAGE);     

        }   

     private String findDistance(int[][] distances, String[] names) {                       
         return null;
     }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2017-11-14 18:46:07

代码语言:javascript
复制
import java.util.*;
import java.lang.*;
import java.io.*;

class Demo6 {
    public static void main(String[] args) {


        int[][] distances = {{0, 50, 100, 20}, {50, 0, 70, 110},
                {100, 70, 0, 200}, {20, 110, 200, 0}};


        String[] names = {"manassas", "fairfax", "baltimore", "centerville"};

        /* Take two more inputs which are FROM city TO city */
        /* **I have taken two default city names below** */
        /* And findDistance method should have four arguements */

        findDistance(distances, names, "baltimore", "centerville");


    }


    private static void findDistance(int[][] distances, String[] names, String a, String b) {

        int x = 0, y = 0;

        /*below i am finding the index of the cities 'a' and 'b'*/
        /*below logic works only if both the cities exist  in the array*/

        for (int i = 0; i < names.length; i++) {
            if (names[i].equals(a))
                x = i;
            if (names[i].equals(b))
                y = i;
        }
    /*after calculating the index just print their cost from the matrix distance*/
    /*its as simple as that*/
        System.out.println(distances[x][y]);

    }
}

希望能有所帮助。我已经在注释中解释了代码。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47283177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档