簡體   English   中英

如何拆分此代碼,以便可以在main方法中調用它並從另一個文件中的方法返回語句?

[英]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?

我需要以某種方式拆分此代碼,即從主調用它並在另一個文件中的另一個方法中進行排序。

這就是我的主要內容:

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

    }

這是另一種方法:

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

使用main方法創建一個類。

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

您的另一堂課:

Sort.java

public class Sort{

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

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM