我想将java中两个数组的数量相乘。我声明了两个数组对象。a 用于获取 xValue,b 用于获取 yValue。在将 x 和 y 的值放入 n 个程序后,每次 x 和 y 的值都应该相乘。请给我看代码.. import java.util.*;
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();
}
}
其实我想做formula。我想得到 x*y 、 x^2 和 y^2 的值。很抱歉,我是 JAVA 语言的新手,不知道该怎么做。