Java 如何让方法重复?爪哇

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19906401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:13:12  来源:igfitidea点击:

How can I make a method repeat itself? Java

javaloopsmethodsiteration

提问by handroski

I have a method that fills an array and I need to find a way to make it repeat a number of times. The purpose is to iterate and reiterate the density of a planet to narrow its mass,gravity and densities at specific points which are concentric shells. This is my first program but, I have learned a decent amount while working on this I think. Thanks everyone

我有一个填充数组的方法,我需要找到一种方法让它重复多次。目的是迭代和重申行星的密度,以缩小其质量、重力和在同心壳特定点的密度。这是我的第一个程序,但是,我认为在进行此工作时我已经学到了相当多的东西。谢谢大家

Here is my code sample of the density calculation. I probably included too much but oh well. So I need to make this iterate selected number of times. Each iteration needs to be put back into the mass calculation which will then be put back into the gravity calculation. And then the show starts again.

这是我的密度计算代码示例。我可能包含的太多了,但是很好。所以我需要让这个迭代选定的次数。每次迭代都需要放回质量计算,然后再放回重力计算。然后节目又开始了。

public class ItrDensityGrid {

    public double itrrho[];
    double b = InitialConditions.bmod;  

    // Iterating grid of densities 

    public ItrDensityGrid(int shells, double radius, double mass){

            GravityGrid gg = new GravityGrid(shells, radius, mass); 

            for(int k = shells; k >= 0; k--){

                    itrrho[k] = (itrrho[k]*(1+(gg.alpha[k]*(1.0 / 2)))*(1 / (1-((gg.alpha[k])*(1.0 / 2)))));

        }
    }
}

回答by Stefan

you could make a function which checks if the tolerances of your calculations are already good enough, here is some "pseudocode"

您可以创建一个函数来检查计算的容差是否已经足够好,这里是一些“伪代码”

 while(toleranceIsGood(planet) == false)
 {
      planet = calculatePlanet(planet);
 }

planet would be the array. of course you can implement things like Endless loop detection etc

行星将是阵列。当然你可以实现无限循环检测等

回答by Haywire

This can be achieved with the help of Recursion, or looping.

这可以在Recursion或循环的帮助下实现。

In recursion, you call the method again from inside of the method itself. Make sure to call (or return) conditionally, otherwise, it may lead to infinite loop!

在递归中,您从方法本身内部再次调用该方法。一定要有条件地调用(或返回),否则可能导致死循环!

Here is an example with recursion:

这是一个递归示例:

public planetMars (double density, double mass) {

    // do your calculations 
    density = density / 10.05312;
    mass = mass / 7.2378;
    myArray[] = density; // or whatever you want        

    // if calculations have not narrowed enough, call recursively
    if ( density > 5.2)
        planetMars (density, mass);
}

alternatively, with loop, you may do something like:

或者,使用 loop,您可以执行以下操作:

public planetMars (double density, double mass) {

    // loop unless the calculation is not cool
    while ( density > 5.2) {
        // do your calculations 
        density = density / 10.05312;
        mass = mass / 7.2378;
        myArray[] = density; // or whatever you want    
    }
}