繁体   English   中英

Java - 将二维数组传递给函数

[英]Java - Passing 2D array to function

-------------------------------------------------------------
|             | 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 - 将二维数组传递给函数

1)为距离定义一个二维数组并填充它->完成(它在程序中显示)

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

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

例如

从城市输入:
输入城市:

让我们在用户输入 cityNames 时使用 JOptionPane 和错误处理功能。

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

5)调用函数“显示”距离<-显示用户输入的那些城市的距离。

我遇到的问题:

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

(跟进#4)

b) 将参数传递给函数显示。 (跟进#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;
    }
}

根据您的问题和给定的代码,我编写了以下课程。 我故意将方法主体留空,因此您需要自己弄清楚其中的一些细节。 如果你可以填充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.
    }
}
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`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM