-------------------------------------------------------------
| | 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)询问用户城市的来源和目的地:
例如
从城市进入:
输入目的地城市:
当用户输入城市名称时,让我们使用 JOptionPane 和错误处理功能。
4)调用函数“findDistance”并将城市名称数组和距离数组传递给该函数。
5)调用功能“显示”距离<- showing the distances of those cities that was entered by user .
我遇到的问题:
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;
}
}
根据您的问题和给定的代码,我编写了以下课程。我故意将方法体留空,因此您需要自己弄清楚其中的一些细节。如果您可以填充
findDistance
和 display
方法的主体,您就可以实现您想要的。
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`