2012-11-21 49 views
-3

我想在java中乘以兩個數組的數字。我聲明瞭兩個數組對象。 a獲得xValue和b獲取y值。在爲n個程序添加x和y的值後,每次x和y的值應該相乘。請給我看代碼.. import java.util。*;在java中的兩個數組中乘以數字

public class DataSetTesterN { 
    public static void main(String[] args) 
    { 
     DataSet a = new DataSet(); 
     // Object "a" for xValue" 
     DataSet b = new DataSet(); 
     // Object "b" for yValue" 
     Scanner input=new Scanner(System.in); 
     System.out.println("enter the total number of Programs"); 
     int m =input.nextInt(); 
       for(int i =1; i <=m; i++) 
        // Entering total number of tested program. 
           { 
      System.out.println("enter x value for the program no. "+i+""); 
      a.add(input.nextInt()); 
      // Getting an input for xValue. 


      System.out.println("enter y value for the program no. "+i+""); 
      b.add(input.nextInt()); 
      // Getting an input for yValue. 


      } 



     System.out.println("count x: " + a.getCount()); 
     System.out.println("count y: " + b.getCount()); 

     System.out.println("Mean x: " + a.getMean()); 
     System.out.println("Mean y: " + b.getMean()); 

     System.out.println("Sum x: " + a.getSum()); 
     System.out.println("Sum y: " + b.getSum()); 

     System.out.println("standard deviation: " + a.getStandardDeviation()); 
     System.out.println("standard deviation: " + b.getStandardDeviation()); 



} 
} 

/////////////////////////////

類爲數據集

import java.util.ArrayList; 
import java.util.List; 


public class DataSet { 

    private List<Double> inputList = new ArrayList(); 
    double x = 0; 

    public DataSet() { 
    } 

    public void add(double x) { 

     inputList.add(x); 

    } 


    public double getMean() { 

     double sum = getSum(); 
     double count = getCount(); 
     double mean = sum/count; 

     return mean; 

    } 

    public double getSum() { 
     double sum = 0; 

     for (double d : inputList) { 
      sum += d; 
     } 
     return sum; 
    } 

    public double getStandardDeviation() { 


     double sum = getSum(); 


     double mean = getMean(); 
     double calc1 = 0; 
     double calc2 = 0; 
     double count = getCount(); 
     double stdDeviation = 0; 

     //System.out.println("Sum = " + sum); 

     for (int i = 0; i < count; i++) { 
      calc1 = inputList.get(i) - mean; 
      calc1 = Math.pow(calc1, 2); 
      calc2 = calc2 + calc1; 
     } 

      calc2 = calc2/(count-1); 
      stdDeviation = Math.sqrt(calc2); 
     return stdDeviation; 
    } 

    public int getCount() { 
     return inputList.size(); 

    } 
} 

其實我想做formaula。我想要得到x * y,x^2和y^2的值。 非常抱歉,我是JAVA語言的新手,不知道該怎麼做。

+2

什麼是DataSet?你想如何增加數組?請發表一個例子。 –

+2

你是什麼意思「乘以兩個數組的數量」?這可以用幾種不同的方式解釋 - 請舉個例子。 – arshajii

+1

「我宣佈了兩個數組對象」 - 無法在代碼示例中看到它們... – Andy

回答

0

對於Java來說,這是如此的新鮮,你使用arraylist來做簡單的算術嗎?這用於什麼?