避免重复的代码

时间:2013-11-26 11:22:56

标签: c# loops

这是我的代码的一部分

double y1 = a / (2 * Math.Sin(j1));
double y2 = a / (2 * Math.Sin(j2));
double y3 = a / (2 * Math.Sin(j3));
double y4 = a / (2 * Math.Sin(j4));
double y5 = a / (2 * Math.Sin(j5));
double y6 = a / (2 * Math.Sin(j6));
double y7 = a / (2 * Math.Sin(j7));
double y8 = a / (2 * Math.Sin(j8));
double y9 = a / (2 * Math.Sin(j9));
double y10 = a / (2 * Math.Sin(j10));
double y11 = a / (2 * Math.Sin(j11));
double y12 = a / (2 * Math.Sin(j12));

y1 - y12j1 - j12都是独立/不同的值,

有没有办法循环所以我不必为

写出一行
y1, y2, y3

3 个答案:

答案 0 :(得分:0)

double y[12];
double j[12] = {...};

for(int i = 0; i<12; i++){
  y[i]=a/(2 * Math.Sin(j[i]));
} 

答案 1 :(得分:0)

给定一个j

数组
var jArray = new double[]{1.0,2.0,3.0};

你可以得到y这样的数组:

var a = 99.99; // or whatever 'a' is
var yArray = jArray.Select(j => a / (2 * Math.Sin(j))).ToArray();

答案 2 :(得分:0)

您可以为这些数字添加数组。

就像是:

double[] myArray = new double[12];
for(int i = 0; i<12; i++){
myArray[i] = a / (2 * Math.Sin(j[i]));
}