我想要爲x = rcos(a)和y = rsin(a)生成半徑爲1的圓的x和y值,但是當我使用三下面的不同方法我只通過手動添加它們來獲得正確的值,但我想將它們添加到循環中。Java ArrayList在使用for循環時存儲不同的值
ArrayList<Double> cCoords = new ArrayList<Double>();
double pi = Math.PI;
double pi_over_two = Math.PI/2.0;
double pi_over_four = Math.PI/4.0;
double pi_over_180 = Math.PI/180.0;
double piVals = 0;
cCoords.add(Math.cos(0));
cCoords.add(Math.sin(0));
cCoords.add(Long.valueOf(Math.round(Math.cos(pi_over_two))).doubleValue());
cCoords.add(Math.sin(pi_over_two));
cCoords.add(Math.sin(pi_over_two+pi_over_two+pi_over_two));
////////Below values are printed correctly
cCoords.add(Math.cos(pi_over_four));
cCoords.add(Math.sin(pi_over_four));
cCoords.add(Math.cos(pi_over_four+pi_over_four));
cCoords.add((Math.sin(pi_over_four+pi_over_four)));
cCoords.add(Math.cos(pi_over_four+pi_over_four+pi_over_four));
cCoords.add(Math.sin(pi_over_four+pi_over_four+pi_over_four));
cCoords.add(Math.cos(pi_over_four +pi_over_four+pi_over_four+pi_over_four));
cCoords.add(Math.sin(pi_over_four +pi_over_four+pi_over_four+pi_over_four));
cCoords.add(Math.cos(pi_over_four+pi_over_four + pi_over_four+pi_over_four+pi_over_four));
cCoords.add(Math.sin(pi_over_four+pi_over_four +pi_over_four+pi_over_four+pi_over_four));
cCoords.add(Math.cos(pi_over_four+pi_over_four+pi_over_four +pi_over_four+pi_over_four+pi_over_four));
cCoords.add(Math.sin(pi_over_four+pi_over_four+pi_over_four + pi_over_four+pi_over_four+pi_over_four));
//////////////Above values are printed correctly
for(int i = 0; i < 6; i++) { // Wrong values
cCoords.add(i, Math.cos(pi_over_four));
cCoords.add(i+1, Math.sin(pi_over_four));
pi_over_four+=pi_over_four;
}
/////
for(int i = 0; i < 6; i++) { // x=1 coordinate gets repeated with different y values
cCoords.add(Math.cos(pi_over_four));
cCoords.add(Math.sin(pi_over_four));
pi_over_four+=pi_over_four;
}
////
for(int i = 0; i < cCoords.size()-1; i+=2) {
System.out.print("(" + cCoords.get(i) + ",");
System.out.println(cCoords.get(i+1) + ")\n");
}
謝謝大家的幫助下,找到了循環應該是類似 您的建議: INT J = 0; for(int i = 0; i <6; i + = 2)cCoords.add(i,Math.cos((j)* pi_over_four)); cCoords.add(i + 1,Math.sin((j)* pi_over_four)); j + = 0.5) – user1420482
@ user1420482您的歡迎! :) – user2004685