在java中查找数组中的max和min

时间:2016-03-12 17:16:40

标签: java arrays function elements

我有wordpad文件。我读了它的元素并写入数组。然后我必须找到它的最大值和最小值然后我找到所有第一列的平均值。然后我采用第一列元素并编写此公式x1=(x1-average)/s // max-min=S,我必须在方法(函数)中编写它。我该怎么做 ?我在哪里写这个方法?这是我的程序代码:

public static void main(String[] args) throws FileNotFoundException {

    double teta0= 0;
    double teta1= 0;
    double teta2= 0;
    double alpha = 0.1;
    double temp0;
    double temp1;
    double temp2;
    double J;
    double h;
    double sumJ;
    double sumTemp0=0;
    double sumTemp1=0;
    double sumtemp2=0;
    double x1 ;

    double array [][] = new double [47][3];

    Scanner sc = new Scanner(new File("C:\\Users\\Grundig\\Downloads\\ex1data2.txt"));

    int k=0;  
    while ( sc.hasNextLine()){
        String s=sc.nextLine();
        array[k][0]=Double.parseDouble(s.split(",")[0]);
        array[k][1]=Double.parseDouble(s.split(",")[1]);
        array[k][2]=Double.parseDouble(s.split(",")[2]);
        k++;
    }

    for(int i=0;i<50;i++){
        sumJ = 0;
        sumTemp0 = 0;
        sumTemp1 = 0;
        double sumTemp2 = 0;

        for (int j=0;j<47;j++){    
            h = teta0 + teta1*array[j][0]+teta2*array[j][1];

            sumJ= sumJ + Math.pow((h-array[j][2]),2);            
            sumTemp0= sumTemp0 + (h-array[j][2]); 
            sumTemp1= sumTemp1 + (h-array[j][2])*array[j][0];  
            sumTemp2= sumTemp2 + (h-array[j][2])*array[j][1];
        }

        J = sumJ/(2*47); 

        System.out.println((i+1)+".J=" +J);

        temp0 = teta0 - alpha*sumTemp0/47;
        temp1 = teta1 - alpha*sumTemp1/47;
        temp2 = teta2 - alpha*sumTemp2/47;
        teta0 = temp0;
        teta1 = temp1;
        teta2 = temp2;

     }
}

0 个答案:

没有答案