如何分割这段代码,以便我可以在main方法中调用它,并从另一个文件中的方法返回语句?

时间:2023-01-22 12:59:21

I need to split this code in a way where I would be calling it from the main and doing the sorting in another method in a different file.

我需要对代码进行拆分,以便从main调用它,并在另一个文件中的另一个方法中进行排序。

This is what I have in the main:

这就是我的主要内容:

    public class Test {
        public static void main(String[] args) {

            //array of 10 numbers
            double numbers[] = new double[]{1.9, 2.5, 3.7, 2, 1.5, 6, 3 , 4 , 5, 2};

            //assign first element of an array to largest and smallest
            double smallest = numbers[0];
            double largetst = numbers[0];

            for(int i=1; i< numbers.length; i++)
            {
                    if(numbers[i] < smallest)
                            smallest = numbers[i];

            }
            System.out.println("The minimum number is: " + smallest);
    }

    }

This is the other method:

这是另一种方法:

    import java.util.Arrays;
    public class Chapter8 {
    public static void chap8method(){}
    public static double[][] sortRows(double[][] m) {
    Chapter8.chap8method();
    double[][] result = Chapter8.sortRows(m);
     }
    }

1 个解决方案

#1


0  

Create a class with the main method.

使用主方法创建一个类。

Main.java

Main.java

public class Main {

    public static void main(String[] args) {

       //if your sort method is static in another class
Sort.sortRows(

//if your method is non static than create an object of that class
Sort obj = new Sort();
obj.sortRows
    }
}

Your another class:

你的另一个类:

Sort.java

Sort.java

public class Sort{

 public static double sortRows(double[][] m) {
//your sorting logic here

    }
}

#1


0  

Create a class with the main method.

使用主方法创建一个类。

Main.java

Main.java

public class Main {

    public static void main(String[] args) {

       //if your sort method is static in another class
Sort.sortRows(

//if your method is non static than create an object of that class
Sort obj = new Sort();
obj.sortRows
    }
}

Your another class:

你的另一个类:

Sort.java

Sort.java

public class Sort{

 public static double sortRows(double[][] m) {
//your sorting logic here

    }
}