我想将java中两个数组的数量相乘。我声明了两个数组对象。 a获取xValue和b获取yValue ..在为n个程序设置x和y的值后,每次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());
}
}
/////////////////////////////
DataSet的类
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();
}
}
其实我想制作公式。我想获得x * y,x ^ 2和y ^ 2的值。 我很抱歉我是JAVA语言的新手,不知道该怎么做。
答案 0 :(得分:0)
对java这么新,你使用arraylist来做简单的算术吗?这是用来做什么的?