使用嵌套循环
我仍在努力实现上述问题所述的目标。基本上我的代码现在是这样的:
for(int ac = 0; ac < gravityObject.length; ac++){
gravField[ac].setVector(0,0);
System.out.println(ac);
for(int ad=0;ad<gravityObject.length;ad++){
System.out.println(ad);
if(ac!=ad){
gravField[ac].increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
}
else{
//do nothing
}
gravField[ac].print();
}
}
这很烦人,我不明白为什么会这样!
我想计算地球因太阳和月亮而产生的加速度。所以我有双[]行星质量={earthMass,sunMass,moonMass},一系列粒子物体粒子[]行星={earth,sun,moon}和一系列由于重力而产生的加速度,它们还没有被计算出来:物理矢量[]gravField={earthGrav,sunGrav,moonGrav}。
我的循环是用来填充这个数组的。我想求和sunGrav和moonGrav来找到地球的加速度,并将结果存储在地球的Grav中,所以在GravField[0]中。然后地球重力,月球重力,这应该被存储为重力场[1]。
我的循环正确地计算的值GravField[0],GravField[1]和GravField[2],然后不幸地设置所有的GravField[]等于GravField[2]。
这大概是因为我一直将gravField[ac]重置为零。但如果我没有,那么我就得不到正确的值。我的主要类的其他部分(问题肯定是循环):公共静态PhysicsVector[]copyArray(PhysicsVector[]a){int length=a.length;PhysicsVector[]copy=new PhysicsVector[length];System.arraycopy(a,0,copy,0,length);return copy;}
public static double sumArray(double[] p){
double sum = 0;
for(int z= 0; z < p.length; z++){
sum += p[z];
}
return sum;
}
public static PhysicsVector[] subtract(PhysicsVector[] diff, PhysicsVector g){
for (int ab=0; ab<diff.length;ab++){
diff[ab].decreaseBy(g);
}
return diff;
}
public static void main(String[] args) throws IOException{
java.io.File file = new java.io.File("output.txt" );
java.io.PrintWriter n = new PrintWriter(file);
//Initialise variables here
PhysicsVector earthInitialPos = new PhysicsVector();
PhysicsVector earthInitialV = new PhysicsVector();
PhysicsVector sunInitialV = new PhysicsVector();
PhysicsVector sunInitialPos = new PhysicsVector();
PhysicsVector moonInitialPos = new PhysicsVector();
PhysicsVector moonInitialV=new PhysicsVector();
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the size of the time step:");
double timeStep = scanner.nextDouble();
//SET PLANETS' INITIAL POSITIONS
//Set Earth's initial position and initial velocity
earthInitialPos.setVector(1.4960*Math.pow(10,11),0);
earthInitialV.setVector(0,29785.5889);
//Set the sun's initial position
sunInitialPos.setVector(0,0);
sunInitialV.setVector(0,-0.08945); //v = 2*pi*R/T
//Set the moon's initial position and velocity
moonInitialPos.setVector(3.844*Math.pow(10,8),0);
moonInitialPos.increaseBy(earthInitialPos);
moonInitialV.setVector(0,1023.2195);
moonInitialV.increaseBy(earthInitialV);
//CREATE GRAVFIELD OBJECTS
GravField sunGravField = new GravField(sunMass, sunRadius, sunInitialPos);
GravField earthGravField = new GravField(earthMass, earthRadius, earthInitialPos);
GravField moonGravField = new GravField(moonMass, moonRadius, moonInitialPos);
//CREATE PARTICLE OBJECTS
Particle earth = new Particle(earthMass, earthInitialPos, earthInitialV);
Particle sun = new Particle(sunMass, sunInitialPos, sunInitialV);
Particle moon = new Particle(moonMass, moonInitialPos, moonInitialV);
double time = 0;
double finalTime = 31557600; //Run for one earth year
//Initialise newV and set equal to initialV
PhysicsVector newV = new PhysicsVector();
newV = earthInitialV;
PhysicsVector sunNewV = new PhysicsVector();
sunNewV = sunInitialV;
PhysicsVector moonNewV = new PhysicsVector();
moonNewV = moonInitialV;
PhysicsVector newSunGrav = new PhysicsVector();
PhysicsVector newEarthGrav = new PhysicsVector();
PhysicsVector newMoonGrav = new PhysicsVector();
GravField[] gravityObject = {earthGravField, sunGravField, moonGravField};
PhysicsVector[] position = {earthInitialPos, sunInitialPos, moonInitialPos};
PhysicsVector[] velocity = {newV, sunNewV, moonNewV};
PhysicsVector[] gravField = {zero, zero, zero};
double[] planetMass = {earthMass, sunMass, moonMass};
Particle[] planets = {earth, sun, moon};
PhysicsVector a = new PhysicsVector(0,0);
//Calculate the centre of mass and subtract position from positions of planets, so c.o.m is at origin
PhysicsVector centreOfMass = new PhysicsVector();
centreOfMass = earth.centreOfMass(planetMass, position);
position = SolarSim.subtract(position, centreOfMass);
//Calculate centre of mass velocity and subtract from planet velocities
PhysicsVector centreOfMassVelocity = new PhysicsVector();
centreOfMassVelocity = earth.cOMVel(planetMass, velocity);
velocity = SolarSim.subtract(velocity, centreOfMassVelocity);
//Calculate fields of planets
for(int ac=0; ac<gravityObject.length; ac++){
gravField[ac].setVector(0,0);
System.out.println(ac);
for(int ad=0;ad<gravityObject.length;ad++){
System.out.println(ad);
if(ac!=ad){
//gravField[ac].increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
a.increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
gravField[ac] = a;
}
else{
//do nothing
}
gravField[ac].print();
}
}
System.out.println(Arrays.toString(gravField));
PhysicsVector[] newP = new PhysicsVector[posithtml" target="_blank">ion.length];
PhysicsVector[] newGrav = {zero,zero,zero};
PhysicsVector[] newVel = new PhysicsVector[velocity.length];
PhysicsVector sum1 = new PhysicsVector(0,0);
PhysicsVector sum2 = new PhysicsVector(0,0);
PhysicsVector sum3 = new PhysicsVector(0,0);
//do{
PhysicsVector[] y = new PhysicsVector[gravField.length];
y=copyArray(gravField);
PhysicsVector[] z = new PhysicsVector[gravField.length];
z=copyArray(gravField);
for(int i=0; i<planets.length;i++){
sum1.setVector(0,0);
for(int s=0;s<z.length;s++){
if(i!=s){
sum1.increaseBy(z[s]);
}
else{
sum1.scale(1);
}
}
newP[i] = planets[i].updatePosition(position[i], velocity[i], timeStep, sum1);
}
newP[2].increaseBy(newP[0]);
//Calculate the centre of mass and subtract position from positions of planets, so c.o.m is at origin
centreOfMass = earth.centreOfMass(planetMass, newP);
newP = SolarSim.subtract(newP, centreOfMass);
for(int j=0; j<gravityObject.length; j++){
for(int l=0;l<gravityObject.length;l++){
if(j!=l){
newGrav[j].increaseBy(gravityObject[j].aDueToGravity(planetMass[j], position[j], position[l]));
}
else{
}
}
}
for(int k=0; k<planets.length; k++){
sum2.setVector(0,0);
sum3.setVector(0,0);
for(int m = 0; m<y.length;m++){
if(k!=m){
sum2.increaseBy(y[m]);
sum3.increaseBy(newGrav[m]);
}
else{
sum2.scale(1);
sum3.scale(1);
}
}
newVel[k] = planets[k].updateVelocity(velocity[k], timeStep, sum2, sum3);
}
newVel[2].increaseBy(newVel[0]);
//Calculate centre of mass velocity and subtract from planet velocities
centreOfMassVelocity = earth.cOMVel(planetMass, newVel);
newVel = SolarSim.subtract(newVel, centreOfMassVelocity);
gravField = copyArray(newGrav);
time+=timeStep;
//n.println(e+" "+z);
//n.println(g+" "+h);
//}while (time<=finalTime);
System.out.println(Arrays.toString(newP));
n.close();
}
}
尝试在单独的步骤中调零gravField:
for(int ac = 0; ac < gravityObject.length; ac++++){
gravField[ac].setVector(0,0);
}
for(int ac = 0; ac < gravityObject.length; ac++){
System.out.println(ac);
/// the rest of your code as above, without setVector(0,0)
问题内容: 我想为迭代嵌套循环添加并发性,但是遇到了麻烦。这个示例使用sync.WaitGroup有什么问题? 我越来越 PRS AAC PRS AAC PRS AAC PRS AAC PRS AAC PRS AAC 因此,您可能会看到它跳过了两个数组的第一个元素,而仅迭代了最后一个元素。任何想法如何解决此问题? 问题答案: 这是一个关闭问题。您需要将值传递到循环内的goroutine中,如下所示
在处理嵌套循环的时候可以中断(break)或继续(continue)外层循环。在这类情形中,循环必须用一 些'label(标签)来注明,并且标签传递给 break/continue 语句。 #![allow(unreachable_code)] fn main() { 'outer: loop { println!("Entered the outer loop");
我试图弄清楚嵌套for循环是如何与JavaScipt中的多维数组一起工作的,但有一点让我有些困惑。以股票为例 这就是我所期望的结果123456。但是,如果我将数字添加到外部数组的末尾: 我仍然得到同样的输出1 2 3 4 5 6?我不明白为什么 输出是一个bcdyz,这是我所期望的。为什么字符串的行为会有所不同?
Python 不仅支持 if 语句相互嵌套,while 和 for 循环结构也支持嵌套。所谓嵌套(Nest),就是一条语句里面还有另一条语句,例如 for 里面还有 for,while 里面还有 while,甚至 while 中有 for 或者 for 中有 while 也都是允许的。 当 2 个(甚至多个)循环结构相互嵌套时,位于外层的循环结构常简称为 外层循环或 外循环,位于内层的循环结构常简
这是我的代码。我遇到的问题是,我希望将HP在我的PHP代码中的数字转换为我的HP HTML代码,以及与Cylinder相同的内容。我已经想好了其他的东西,但说到这一部分我就卡住了
本文向大家介绍MATLAB嵌套循环,包括了MATLAB嵌套循环的使用技巧和注意事项,需要的朋友参考一下 示例 可以嵌套循环,以在另一个迭代任务中执行迭代任务。考虑以下循环: 我们使用2个迭代器来显示abc和中元素的所有组合1:m,从而得出: 我们还可以使用嵌套循环来组合每次要完成的任务和几次迭代中要完成的任务: 这里我们要计算所有的斐波那契数列,但是n每次只显示第一个元素,所以我们得到 我们可以做