2016-03-18 79 views
1

這是一個程序,通過在屏幕上移動烏龜來創建分形。爲什麼我的程序中的烏龜不會移動?任何幫助將不勝感激!我很確定這是一個簡單的修復,但我找不到它。爪哇龜圖形動畫基本

感謝

/** 
    * This program is to create a fractal 
    */ 

import java.awt.*; 
class FractalEngine 
{ 
    World world = new World(700,700); 
    Turtle turt = new Turtle(1,1,world); 

public void drawFractal(String rule) 
{ 
    turt.penUp(); 
    turt.moveTo(350,350); 

    int lengthofRule = rule.length(); 

    int start = 0; 
    int fin = 1; 
    String tinyChar; 

    for(int a = 0; a <= lengthofRule; a++) 
    { 
     if(start == 9) 
      tinyChar = rule.substring(start); 
     else 
      tinyChar = rule.substring(start,fin); 

     if(tinyChar == "F") 
      turt.forward(25); 
     else if(tinyChar == "-") 
      turt.turnLeft(); 
     else if(tinyChar == "+") 
      turt.turnRight(); 
     else 
      break; 
     start++; 
     fin++; 

      System.out.println(tinyChar); 
     } 
    } 
} 
public class FractalEngineTester 
{ 
public static void main(String[] args) 
{ 
    FractalEngine fEng = new FractalEngine(); 



    String rule = "F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F"; 
    //String rule = "F-F+F+F-F"; 

    fEng.drawFractal(rule); 
    //String rule = "F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F"; 
    //String rule = "F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F-F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F+F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F+F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F-F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F"; 
} 
} 

回答

0

這段代碼的問題無關海龜 - 它是所有關於字符和字符串。

這不起作用:

String tinyChar = rule.substring(start, fin); 

if (tinyChar == "F") 

你混合Stringchar邏輯。你要麼需要做的:

String tinyChar = rule.substring(start, fin); 

if (tinyChar.equals("F")) # use a method to do the String comparison 

或做:

char tinyChar = rule.charAt(start); 

if (tinyChar == 'F') // notice the change in quotes 

下面是我的代碼,以解決這個問題和其他問題的返工。我使用的是不同的龜包,並簡化了的東西,所以它會爲你的情況下直接一些修改不起作用:

import java.awt.*; 

class FractalEngine 
{ 
    Turtle turt = new Turtle(); 

    public void drawFractal(String rule) 
    { 
     int lengthofRule = rule.length(); 

     for (int position = 0; position < lengthofRule; position++) 
     { 
      char tinyChar = rule.charAt(position); 

      if (tinyChar == 'F') 
       turt.forward(25); 
      else if (tinyChar == '-') 
       turt.left(90); 
      else if (tinyChar == '+') 
       turt.right(90); 
      else 
       break; 
     } 
    } 

    public static void main(String[] args) 
    { 
     FractalEngine fEng = new FractalEngine(); 

     String rule = "F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F"; 

     fEng.drawFractal(rule); 
    } 
} 

輸出

enter image description here

當問一個問題像這樣,確保指出你的烏龜圖形庫來自哪裏,因爲它們有幾個,它們不完全相同。