-2

我应该如何编写代码以在 main 方法中调用“displayMapData”。我非常感谢您的帮助。

如果您提供代码会很有帮助,这样我就可以从错误中吸取教训。

package stormtroopers;
import java.text.DecimalFormat;
public class StormTroopers
{

    public static double eucldeanDistance(double x2, double x1, double 2, double y1)
        {
            double d = 0;
            double xDif = Math.pow(x2-x1,2);
            double yDif = Math.pow(y2-y1,2);
            d = Math.sqrt(xDif + yDif);
            return d;
        }
    public static String distanceBetweenLocations(double x, double y, double [][] locations)
        {
            String s = "\t";
            double totalDistance = 0;
            DecimalFormat df = new DecimalFormat("#.##");
            for(int i=0; i < locations.length; i++)
                {
                    double d = eucldeanDistance(x,locations[i][0],y,locations[i][1]);
                    totalDistance += d;
                    s = s + df.format(d) + "\t";
                }
            return s + df.format(totalDistance);
        }
    public static void displayMapData(double[][] locations,String author)
        {
            System.out.println("___________________STORMTROOPER DISTANCE DATA____________________");
            System.out.println("\tST-1\t" + "\tST-2\t" + "\tST-1\t" + "\tST-4\t" + "\tST-5\t" + "\tST-6\t" + "\tST-7\t" + "\tST-8\t" + "\tST-9\t" + "\tST-10\t" + "TOTAL DISTANCE");
            System.out.println();
            for(int i = 0; i < locations.length; i++)
                 {
                      String s = distanceBetreenLocations(locations[i][0],locations[i][1],locations);
                      System.out.println("ST-" + (i + 1) + s);
                      System.out.println();
                 }
            System.out.println("REPORT DATA CREATED BY " + author + "______________________");
        }
    public static void main(String[] args)
        {
            System.out.println(displayMapData(double[][] locations,String author);
        }
}
4

2 回答 2

1

考虑到你的调用main应该displayMapData()是一个实际调用,而不是一个方法声明,它应该有数据(一个二维数组和一个字符串)而不是变量声明。

就像是:

double[][] data = {{0.5, 5.8, 2.3},
                   {5.2, 3.4, 0.0},
                   {1.2, 1.6, 2.0}};
System.out.println(displayMapData(data, "Me");
于 2019-09-18T18:52:17.030 回答
0

这样做,它会工作,你可能想在答案之间做间隔并改变你的名字

    import java.text.DecimalFormat;

    public class StormTrooper {

    public static double euclideanDistance(double x2, double x1, double y2, double y1){
    double d = 0; 
    double xDif = Math.pow(x2-x1,2);
    double yDif = Math.pow(y2-y1,2);
    d = Math.sqrt(xDif + yDif);
    return d;
    }

    public static String distanceBetweenLocations (double x, double y, double[][] locations){
    String s = "\t";
    double totalDistance = 0; 
    DecimalFormat df = new DecimalFormat("#.##"); 
    for (int i = 0; i < locations.length; i++){
        double d = euclideanDistance(x,locations[i][0],y,locations[i][1]);
        totalDistance += d; 
        s = s + df.format(d) + "\t";
    }
    return s + df.format (totalDistance); 
    }

    public static void displayMapData(double[][] locations, String author){
    System.out.println("________________________STORMTROOPER DISTANCE DATA_____________________________");
    System.out.println("\tST-1\t" + "\tST-2\t" +"\tST-3\t" +"\tST-4\t" +"\tST-5\t" +"\tST-6\t" 
    +"\tST-7\t" 
                        + "\tST-8\t" +"\tST-9\t" +"\tST-10\t" + "TOTAL DISTANCE");
    System.out.println();
    for(int i = 0; i < locations.length; i++){
        String s = distanceBetweenLocations(locations[i][0], locations[i][1], locations);
        System.out.println("ST-" + (i + 1) + s); 
        System.out.println();
    }
    System.out.println("REPORN DATA CREATE BY" + author + 
    "_____________________________________________");
    }

    public static void main(String[] args){
        double[][] inputs = { { 32, 16 }, { 32, 14 }, { 33, 13 }, {35, 13}, { 36, 13 }, { 37, 15 }, {36 ,16}, {35 ,16.5}, {34 ,15}, { 33,16 }};
        String author = "Mayank";
        displayMapData(inputs, author);
    }
    }

在此处输入图像描述

于 2019-09-19T19:08:27.070 回答