繁体   English   中英

如何使用递归工作进行反向补充方法?

[英]How can I make a reverse complement method with recursion work?

我正在尝试创建一个“龙曲线”,它使用左转和右转在绘图面板上创建一个形状。 方向是递归生成的,对于一定数量的迭代或“级别”,方向将前一串转弯添加到单个右转加上前一串的反向补码:前一串+“R”+反向补码上一个字符串。

根据我的结果,我的补码方法没有正确进行反向补码,我不确定为什么。 这是我的代码。

public static void main (String[] args)
   {
      Scanner scan = new Scanner(System.in);
      intro();
      int level = userlevel(scan);
      int size = userSize(scan);
      String fileName = BASE + level + TXT;
      recursion(level, fileName);
      drawDragon(fileName, size, level);
   }

   /**
   * Prints the introductory message
   **/
   public static void intro()
   {
      System.out.println("This program will generate a fractal called the Dragon Curve");
      System.out.println("first explored by John Heighway, Bruce Banks, and William Harter");
      System.out.println("at Nasa in teh 1960's");
   }

   /**
   * Inputs the user's desired level for the dragon curve
   * @param the Scanner object for recieving user inputs
   * @return the user's desired level
   **/
   public static int userlevel(Scanner scan)
   {
      String levelPrompt = "Enter the level of the fractcal you'd like to see (1-25): ";
      int level = MyUtils.getNumber(scan, levelPrompt, MINLEVEL, MAXLEVEL);
      return level;
   }

   /**
   * Inputs the user's desired drawing panel width and height
   * @param the Scanner object for recieving user inputs
   * @return the user's desired drawing panel size
   **/
   public static int userSize(Scanner scan)
   {
      String panelPrompt = "Enter the size of your drawing panel, in pixels (100-2000): ";
      int size = MyUtils.getNumber(scan, panelPrompt, MINSIZE, MAXSIZE);
      return size;
   }

    /**
    * Creates the reverse complement of the previous dragon curve string
    * @param the previous string used the in the recursive loop
    * @return the new reverse complement string
    **/  
   public static String complement(String previousString)
   {
      StringBuilder newString = new StringBuilder();
      char letter = '\0';
      for (int i=previousString.length(); i<0; i--)
      {
         letter = previousString.charAt(i);
         if (letter == 'L')
            newString.append('R');
         else if (letter == 'R')
            newString.append('L');
      }

      return newString.toString();
   }

   /** 
   * Creates the string of directions for the dragon curve using recursion
   * @param level - the user's desired level
   * @param fileName - the file in which the dragon curve string is located
   * @return the new set of directions for the curve after each loop
   **/
   public static String recursion(int level, String fileName)
   { 
      PrintStream output = MyUtils.createOutputFile(fileName);
      String newDirection = "";
      String directions = "";
      if (level==1)
      {
         directions = "R"; 
         output.print(directions);
      }  
      else
      {
         String previousString = recursion(level-1, fileName);
         String nextComplement = complement(previousString);
         newDirection = previousString + "R" + nextComplement;
         output.print(newDirection);
         System.out.println(newDirection);
         //output.flush();
         //output.close();
      }
      return newDirection;
   }

   /**
   * Draws the dragon curve on the drawing panel
   * @param fileName - the file where the dragon curve directions are located
   * @param size - the width and height of the drawing panel
   * @param level - the user's desired level
   **/
   public static void drawDragon(String fileName, int size, int level)
   { 
      Scanner fileScan = MyUtils.getFileScanner(fileName);
      System.out.println("Path generated, writing to file " + fileName + "...");
      String direction = fileScan.nextLine();
      System.out.println("Drawing Curve...");
      DragonDraw dd = new DragonDraw(size);
      dd.drawCurve(fileName, level);
   }
}

这是错误:我只得到重复的“R”。

Enter the level of the fractcal you'd like to see (1-25): 20
Enter the size of your drawing panel, in pixels (100-2000): 500
R
RR
RRR
RRRR
RRRRR
RRRRRR
RRRRRRR
RRRRRRRR
RRRRRRRRR
RRRRRRRRRR
RRRRRRRRRRR
RRRRRRRRRRRR
RRRRRRRRRRRRR
RRRRRRRRRRRRRR
RRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRR
Path generated, writing to file dragon20.txt...
Drawing Curve...

非常感谢!

看看你的for循环里面的complement方法。 它在变量i小于零时运行,这应该在变量大于零时运行。 所以把这个for (int i=previousString.length(); i<0; i--)改成for (int i=previousString.length() - 1; i >= 0; i--)就可以了.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM