在Java中如何输出缩进字符串?

huangapple go评论97阅读模式
英文:

How to output indenting strings in Java?

问题

我正在做汉诺塔问题,我想输出所有移动步骤(2^n-1),并且逐步增加前导空格。

例如:

  1. 从顶部移动盘子从...
  2. 从顶部移动盘子从...
  3. 从顶部移动盘子从...
  4. 从顶部移动盘子从...

...以此类推。

我尝试创建一个单独的“space”方法,但不确定如何将其实现到程序中。

这是我目前代码的一部分:

  1. public static void towersOfHanoi(int disk, int source, int dest){
  2. int temp;
  3. if (disk == 1) {
  4. moveDisk(source,dest);
  5. }
  6. else {
  7. temp = 6 - source - dest;
  8. towersOfHanoi(disk-1,source,temp);
  9. moveDisk(source,dest);
  10. towersOfHanoi(disk-1,temp,dest);
  11. }
  12. }
  13. private static void moveDisk(int source, int dest) {
  14. System.out.println("从 " + source + " 移动顶部盘子到 " + dest);
  15. }
英文:

I am doing the Towers of Hanoi and I want to output all the moves (2^n-1) with increasing leading spaces.
E.g.

  1. Moving top disk from....
  2. Moving top disk from....
  3. Moving top disk from....
  4. Moving top disk from....

... and so on.

I tried to create a separate "space" method, but I'm unsure as to how to implement it into the program

This is a part of my code right now.

  1. public static void towersOfHanoi(int disk, int source, int dest){
  2. int temp;
  3. if (disk == 1) {
  4. moveDisk(source,dest);
  5. }
  6. else {
  7. temp = 6 - source - dest;
  8. towersOfHanoi(disk-1,source,temp);
  9. moveDisk(source,dest);
  10. towersOfHanoi(disk-1,temp,dest);
  11. }
  12. }
  13. private static void moveDisk(int source, int dest) {
  14. System.out.println("Moving top disk from " + source + " to " + dest);
  15. }

答案1

得分: 0

你可以通过Apache Commons Framework.import org.apache.commons.lang3.StringUtils

然后使用:

  1. StringUtils.leftPad("123456", tab_length * 2)
  2. //或者以你希望格式化左侧空间的方式

这是最方便的方法,否则你可以编写一个自定义的leftPad方法,使用StringBuilder或类似的方法。

英文:

You can import org.apache.commons.lang3.StringUtils via the Apache Commons Framework.

Then use:

  1. StringUtils.leftPad("123456", tab_length * 2)
  2. //or however you want to format the left space

This is the most convenient way, otherwise, you can write a custom leftPad method using StringBuilder or something similar.

答案2

得分: 0

我已使用字符串构建器来实现缩进。缩进的数量由类“indents”的静态变量定义,并且每次调用moveDisk方法时都使用for循环。

  1. public class Test {
  2. static Integer indents = -2;
  3. static String indent = " ";
  4. public static void towersOfHanoi(int disk, int source, int dest){
  5. int temp;
  6. if (disk == 1) {
  7. moveDisk(source,dest);
  8. }
  9. else {
  10. temp = 6 - source - dest;
  11. towersOfHanoi(disk-1,source,temp);
  12. moveDisk(source,dest);
  13. towersOfHanoi(disk-1,temp,dest);
  14. }
  15. }
  16. private static void moveDisk(int source, int dest) {
  17. indents = indents + 1;
  18. StringBuilder sb = new StringBuilder();
  19. for(int i = 0; i<=indents; i++){
  20. sb.append(indent);
  21. }
  22. sb.append("Moving top disk from ");
  23. sb.append(source);
  24. sb.append( " to ");
  25. sb.append( dest);
  26. System.out.println(sb.toString());
  27. }
  28. public static void main(String[] args) {
  29. moveDisk(10, 10);
  30. moveDisk(1, 10);
  31. moveDisk(20, 10);
  32. }
  33. }

结果:

  1. Moving top disk from 10 to 10
  2. Moving top disk from 1 to 10
  3. Moving top disk from 20 to 10
英文:

I have used a string builder to implement the indents. The number of indents are defined by a static variable for the class "indents" and a for loop is used each time the moveDisk method is called.

  1. public class Test {
  2. static Integer indents = -2;
  3. static String indent = &quot; &quot;;
  4. public static void towersOfHanoi(int disk, int source, int dest){
  5. int temp;
  6. if (disk == 1) {
  7. moveDisk(source,dest);
  8. }
  9. else {
  10. temp = 6 - source - dest;
  11. towersOfHanoi(disk-1,source,temp);
  12. moveDisk(source,dest);
  13. towersOfHanoi(disk-1,temp,dest);
  14. }
  15. }
  16. private static void moveDisk(int source, int dest) {
  17. indents = indents + 1;
  18. StringBuilder sb = new StringBuilder();
  19. for(int i = 0; i&lt;=indents; i++){
  20. sb.append(indent);
  21. }
  22. sb.append(&quot;Moving top disk from &quot;);
  23. sb.append(source);
  24. sb.append( &quot; to &quot;);
  25. sb.append( dest);
  26. System.out.println(sb.toString());
  27. }
  28. public static void main(String[] args) {
  29. moveDisk(10, 10);
  30. moveDisk(1, 10);
  31. moveDisk(20, 10);
  32. }
  33. }

The outcome:

  1. Moving top disk from 10 to 10
  2. Moving top disk from 1 to 10
  3. Moving top disk from 20 to 10

huangapple
  • 本文由 发表于 2020年8月13日 13:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63388895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定