简体   繁体   中英

How do split this code so that I can call it in the main method and return the statement from a method in a different file?

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.

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);
     }
    }

Create a class with the main method.

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

public class Sort{

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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