-------------------------------------------------------------
| | 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)定义距离的2D数组并填充它 - >完成(在节目中显示)
2)定义城市数组 - >完成(在节目中显示)
3)询问用户该城市的来源和目的地:
e.g。
从城市输入:
输入到城市:
当用户输入cityNames时,可以使用JOptionPane和错误处理功能。
4)调用函数“findDistance”并传递citynames数组,该函数的距离数组。
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;
}
}
答案 0 :(得分:0)
根据您的问题和给定的代码,我写了下面的课程。我故意将方法体留空,所以你需要自己弄清楚一些细节。如果您可以填充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.
}
}
答案 1 :(得分:0)
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`