2015-03-03 60 views
0

這是我的主要方法。我有一個ArrayList爲食草動物和我的植物在(1000 x 1000)的畫布大小我有他們做的是: 所有的食草動物吃一種植物,是最接近他們。我知道需要做的是一旦所有的植物都被吃掉了,他們的主要方法應該退出並打印一些統計數據。我的理解是,在我的食草動物開始尋找下一個植物之前,我需要退出,因爲它會給我一個錯誤,因爲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 

回答

0

點是辭掉main方法,但是退出的飲食行爲的循環,以打印一些統計數據。要做到這一點的一個好方法是改變你的循環條件,使其終止時,沒有必要:

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

這將導致你的代碼的其餘部分的outter循環後執行。

作爲您的問題的補充,您可以在需要時使用break語句來獲得循環外部結果,通常與循環內的條件語句if/else if/else結合使用。如上所述,System.exit(0)將導致您的程序終止的實際效力,即而不是

+0

謝謝!這很有道理! – Michelle 2015-03-03 02:19:21

+0

不客氣。如果你認爲這個答案是最好的,那麼考慮投票答案最好,或者儘可能提高答案。 – 2015-03-03 02:21:47

1

雖然有草食動物和植物你可以改變你的while循環只運行。

while(theHerbivores.size() > 0 && thePlants.size() > 0){ 
0

嗯,就我的理解,您正試圖在main方法結束後打印一些信息而不終止整個程序。如果這是正確的,你應該創建一個打印這些統計數據的方法。然後在main方法的末尾調用這個方法。這樣,主方法在調用此方法後結束,但程序繼續運行直到打印統計信息。

下面是一個例子:

public static void main(String[] args) { 
    //everything in the main method 
    //the end of the main method 
    printStatistics(); 
    //main method terminates here but the program runs until the printStatistics() method finishes running 
} 

private void pintStatistics() { 
    System.out.println(/*insert statistics here*/); 
    //entire program terminates here 
}