Java-将2D数组传递给函数

55ooxyrt  于 2022-10-01  发布在  Java
关注(0)|答案(2)|浏览(143)
-------------------------------------------------------------
|             | herndon | fairfax | baltimore | centerville |
-------------------------------------------------------------
|   herndon   |    0    |    50   |    100    |     20      |
-------------------------------------------------------------
|   fairfax   |   50    |    0    |    70     |     110     |
-------------------------------------------------------------
|  baltimore  |   100   |    70   |     0     |     200     |
-------------------------------------------------------------
| centerville |   20    |    110  |    200    |      0      |
-------------------------------------------------------------

Java-将2D数组传递给函数

1)定义距离的二维数组并填充它->完成(如程序所示)

2)定义城市数组->完成(程序中显示)

3)询问用户所在城市的来源和目的地:

例如:

从城市输入:
输入城市:

让我们在用户输入ciyNames时使用JOptionPane和错误处理函数。

4)调用函数findDistance,并将城市名称数组、距离数组传递给该函数。

5)呼叫功能“Display”Distance<-显示用户输入的城市距离。

我遇到了一些问题:

A)将城市名称数组和距离数组传递给findDistance函数

(跟进#4)

B)将参数传递给函数Display。(跟进#5)

这就是我到目前为止所做的!

package test;

class Test1 {
    public static void main(String[] args) {
        int distances[][] = {{0, 50, 100, 20}, {50, 0, 70, 110}, {100, 70, 0, 200}, {20, 110, 200, 0}}; // step 1
        String CityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};  // step 2
        findDistance(distances); // ->  a . 1st problem , i am having problem to pass citynames and distances to this function.
        // display  ()     // -> b .  2nd problem i am having : i have a  issue to pass the arguments to this function to Display Distances between 2 cities .
    public static void findDistance(String[] names, int[] dist, int[] src) {
        for(int i = 0; i < src.length; i++) {
            // i would like to use JOptioanPane function and error handling
            // instead of system.out.println.
            System.out.println("enter from city" + src[i]);
        }
        for(int i = 0; i < dist.length; i++) {
            System.out.println("enter from city" + dist[i]);
        }
        int i = 0;
        dist[i] = src[i];
        return;
    }
}
tgabmvqs

tgabmvqs1#

根据您的问题和给定的代码,我编写了以下类。我故意将方法体留空,因此您需要自己弄清楚其中的一些细节。如果您可以填充findDistancedisplay方法的主体,您就可以完成您想要的事情。

package Test;

import java.util.Scanner;

class Test1 {
    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 CityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};
        Scanner keyboard = new Scanner(System.in);
        System.out.println("enter from city:");
        String src = keyboard.nextLine();
        System.out.println("enter to city:");
        String dest = keyboard.nextLine();
        int distance = findDistance(CityNames, distances, src, dest);
        display(distance);
    }

    public static int findDistance(String[] CityNames, int[][] distances, String src, String dest) {
        // Find and return the distance between src and dest.
        return 0;
    }

    public static void display(int distance) {
        // Display the distance to user.
    }
}
kknvjkwl

kknvjkwl2#

package test;

import javax.swing.JOptionPane;

class ArrayTest {
    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 cityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};

        int departureIndex = findCityIndex("departure", cityNames);
        int destinationIndex = findCityIndex("destination", cityNames);
        display(departureIndex, destinationIndex, distances);
    }

    private static int findCityIndex(String cityType, String[] cityNames) {

        boolean cityNotFound = true;
        int index = 0;

        while(cityNotFound)
        {
            String cityName= JOptionPane.showInputDialog("Please enter " + cityType + " city");

            for (int i = 0; i < cityNames.length; i++)
            {
                if (cityName.equalsIgnoreCase(cityNames[i]))
                {
                    index = i;
                    cityNotFound = false;
                }

            }
            if (cityNotFound)
                JOptionPane.showMessageDialog(null, "Invalid city name. Please reenter.");
        }
        return index;
    }

    private static void display(int departureIndex, int destinationIndex, int[][] distances) {

        JOptionPane.showMessageDialog(null, "Distance is " + distances[departureIndex][destinationIndex]);
    }

}`enter code here`

相关问题