如何退出我的主要方法?

蜜雪儿

这是我的主要方法。我在(1000 x 1000)的画布大小中有一个食草动物和植物的ArrayList,它们的作用是:所有食草动物都吃离它们最近的植物。我知道需要做的是一旦所有植物都吃光了,它们的主要方法应该退出并打印一些统计数据。我了解的是,我需要在我的草食动物开始寻找下一个植物之前退出,因为它会给我一个错误,因为arrayList现在为空。但是,如果我使用“ System.exit(0);” 在我的findNearestWithinFiftyMeters方法之前,它将执行我的其余代码。如何退出但仍然打印我的统计信息?

import java.util.ArrayList;

public class TestDriver {
    public static void main(String[] args){
        //Set the canvas size to (1000,1000) and x and y scale to (0,1000)
        StdDraw.setCanvasSize(1000,1000);
        StdDraw.setXscale(0.0 , 1000.0);
        StdDraw.setYscale(0.0 , 1000.0);

        //ArrayList for the Plants
        ArrayList<Plant> thePlants = new ArrayList<Plant>();
        //Create 300 Plants

        for (int i=0; i<300; i++){
            thePlants.add(new Plant (1000 * Math.random(),1000 * Math.random()));
        }



        //ArrayList for the Herbivores
        ArrayList<Herbivore> theHerbivores = new ArrayList<Herbivore>();
        //Create 20 Herbivores
        for (int i=0; i<20; i++){
            theHerbivores.add(new Herbivore (1000 * Math.random(), 1000 * Math.random()));
        }

        //Draw Graphics:
        while(theHerbivores.size() > 0){
            //Clears the board
            StdDraw.clear();

            //created Herbivores of size
            for(int i=0; i<theHerbivores.size(); i++){  
                //Draws the herbivore at position i
                theHerbivores.get(i).draw();

                if(thePlants.size() == 0){
                 StdDraw.clear();
                }

                //Finds the plant that is closest to the herbivore
                Plant closest = findNearestWithinFiftyMeters(thePlants , theHerbivores.get(i));
                if(closest != null){
                    //IF the closest plant is NOT null then move towards closest plant
                    theHerbivores.get(i).moveToward(closest.getX(), closest.getY() );
                    //now that we have the closest plant in regards to this herbivore
                    //we want it to move to the plant
                    theHerbivores.get(i).createRandomTarget();
                    //Reset the target each time it finds a new plant to chew
                }
                else{
                    //if it IS null
                    //Walk in a random direction (towards the imaginary plant)
                    theHerbivores.get(i).move();
                }
            }

            //Draws plants
            for(Plant p: thePlants){
                p.draw();
            }

            //Check for any herbivores that have moved off screen
            for(int i=0; i < theHerbivores.size(); i++){
                //if an herbivore moved too far left or right move to other side of screen
                if(theHerbivores.get(i).getX()>1000){
                    theHerbivores.get(i).setX(0);
                }
                else if(theHerbivores.get(i).getX()<0){
                    theHerbivores.get(i).setX(1000);
                }               
                //if an herbivore moved too far up or down
                if(theHerbivores.get(i).getY()>1000){
                    theHerbivores.get(i).setY(0);
                }
                else if(theHerbivores.get(i).getY()<0){
                    theHerbivores.get(i).setY(1000);
                }
            }


            //looping through all the plants to remove plants that have been eaten
            for(int i=0; i< theHerbivores.size(); i++){
                for(int j = 0; j < thePlants.size(); j++){
                    if(thePlants.get(j).distanceTo(theHerbivores.get(i).getX(),theHerbivores.get(i).getY()) < 3){

                        thePlants.remove(j);

                        theHerbivores.get(i).eat();
                        //INCREMENT HERBIVORE EATEN COUNT
                    }
                }
                StdDraw.show(1);

            }
            stepCounter++;
        }//end while loop

        System.out.println(stepCounter + " steps done in this simulation");
        long estimatedTime = System.nanoTime() - startTime;
        System.out.println(estimatedTime + "Length of time simulation used" );
        for(int i=0; i<theHerbivores.size(); i++){
            System.out.println("Herbivore # " + i + "X: " + theHerbivores.get(i).getX() + "Y: " + theHerbivores.get(i).getY() + " EATEN: "+ theHerbivores.get(i).getEatCount());
        }

        return;
    } // end of main method


    static long startTime = System.nanoTime(); 
    static int stepCounter = 0;

    public static Plant findNearestWithinFiftyMeters(ArrayList<Plant> thePlants , Herbivore eater){
        //plant variable for storage to find closest plant to that herbivore
        Plant closest = thePlants.get(0);
        for(int i=0; i<thePlants.size(); i++){
            if(eater.distanceTo(closest.getX(), closest.getY()) > eater.distanceTo(thePlants.get(i).getX(),thePlants.get(i).getY()) ){
                //if the plant in closest variable is farther away than the 
                //plant in index 'i' then replace the plant in closest with the plant in index 'i'
                closest = thePlants.get(i);
            }
        }

        if(eater.distanceTo(closest.getX(),closest.getY()) > 50){
            //if distance is greater than 50(herbivore sight range) then set closest equal to null
            closest=null;
        }
        return closest;
    }   
} // end of class
尼克·L。

关键不是退出您的main方法,而是退出进餐操作的循环以打印一些统计信息。做到这一点的一种好方法是更改​​循环条件,以使其在不需要时终止:

while(theHerbivores.size() > 0 && thePlants.size() > 0) {
 //Rest of code....
}

这将导致在外部循环之后执行其余代码。

除了您的问题外,您还可以break在需要时使用一条语句来使之离开循环,通常将其与if/else if/else循环中的条件语句结合使用如前所述,System.exit(0)将导致程序终止的实际作用力,这不是您所需要的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在主要方法上打印我的方法以显示输出?

来自分类Dev

我如何拥有主要方法和WebSockets

来自分类Dev

我应该如何添加没有主要方法的项目?

来自分类Dev

我如何退出循环?

来自分类Dev

我的Rational代码的主要方法

来自分类Dev

如何退出方法

来自分类Dev

如何将我的方法固定在主要方法上显示?

来自分类Dev

我如何从另一个方法C#转到主要方法

来自分类Dev

如何将我的方法固定在主要方法上显示?

来自分类Dev

如何使我的<aside>与主要部分的高度相同?

来自分类Dev

如何结束我的主要功能?

来自分类Dev

如何让 X 忽略我的主要 GPU?

来自分类Dev

退出时如何调用方法?

来自分类Dev

javafx主要方法launch(args)如何工作?

来自分类Dev

Java如何被JVM调用的主要方法

来自分类Dev

如何从Java中的主要函数访问方法

来自分类Dev

退出框架时如何关闭我的程序

来自分类Dev

满足我的条件时如何退出循环?

来自分类Dev

如何从主要功能之外及早退出Rust程序?

来自分类Dev

即使当前活动不是主要活动,如何退出Android应用

来自分类Dev

如何从主要活动中按退键退出应用程序?

来自分类Dev

我们可以更改Java的主要方法吗?

来自分类Dev

为什么我的代码看不到其主要方法?

来自分类Dev

如何从一个方法中退出,即我如何从这个递归中的函数返回到 java 中?

来自分类Dev

Android如何从我的主要活动中调用片段

来自分类Dev

我如何将customView连接到主要活动

来自分类Dev

我如何知道哪个“ explorer.exe”进程是主要进程?

来自分类Dev

如何将我的主要博客内容居中?

来自分类Dev

我如何让Qt Creator给我程序的退出代码?

Related 相关文章

热门标签

归档