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

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

How to output indenting strings in Java?

问题

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

例如:

    从顶部移动盘子从...
        从顶部移动盘子从...
            从顶部移动盘子从...
                从顶部移动盘子从...

...以此类推。

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

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

public static void towersOfHanoi(int disk, int source, int dest){
    int temp;
    if (disk == 1) {
        moveDisk(source,dest);
    }
    else {
        temp = 6 - source - dest;
        towersOfHanoi(disk-1,source,temp);
        moveDisk(source,dest);
        towersOfHanoi(disk-1,temp,dest);
    }
}

private static void moveDisk(int source, int dest) {
    System.out.println("从 " + source + " 移动顶部盘子到 " + dest);
}
英文:

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

Moving top disk from....
    Moving top disk from....
        Moving top disk from....
            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.

public static void towersOfHanoi(int disk, int source, int dest){
		int temp;
		if (disk == 1) {
			moveDisk(source,dest);
		}
		else {
			temp = 6 - source - dest;
			towersOfHanoi(disk-1,source,temp);
			moveDisk(source,dest);
			towersOfHanoi(disk-1,temp,dest);
		}
	}
	
	private static void moveDisk(int source, int dest) {
		System.out.println("Moving top disk from " + source + " to " + dest);
	}

答案1

得分: 0

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

然后使用:

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

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

英文:

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

Then use:

StringUtils.leftPad("123456", tab_length * 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循环。

public class Test {

    static Integer indents = -2;
    static String indent = "   ";
    public static void towersOfHanoi(int disk, int source, int dest){
        int temp;
        if (disk == 1) {
            moveDisk(source,dest);
        }
        else {
            temp = 6 - source - dest;
            towersOfHanoi(disk-1,source,temp);
            moveDisk(source,dest);
            towersOfHanoi(disk-1,temp,dest);
        }
    }

    private static void moveDisk(int source, int dest) {

        indents = indents + 1;

        StringBuilder sb = new StringBuilder();

        for(int i = 0; i<=indents; i++){
            sb.append(indent);
        }
        sb.append("Moving top disk from ");
        sb.append(source);
        sb.append( " to ");
        sb.append( dest);

        System.out.println(sb.toString());
    }

    public static void main(String[] args) {

        moveDisk(10, 10);
        moveDisk(1, 10);
        moveDisk(20, 10);

    }
}

结果:

Moving top disk from 10 to 10
   Moving top disk from 1 to 10
      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.

public class Test {

    static Integer indents = -2;
    static String indent = &quot;   &quot;;
    public static void towersOfHanoi(int disk, int source, int dest){
        int temp;
        if (disk == 1) {
            moveDisk(source,dest);
        }
        else {
            temp = 6 - source - dest;
            towersOfHanoi(disk-1,source,temp);
            moveDisk(source,dest);
            towersOfHanoi(disk-1,temp,dest);
        }
    }

    private static void moveDisk(int source, int dest) {

        indents = indents + 1;

        StringBuilder sb = new StringBuilder();

        for(int i = 0; i&lt;=indents; i++){
            sb.append(indent);
        }
        sb.append(&quot;Moving top disk from &quot;);
        sb.append(source);
        sb.append( &quot; to &quot;);
        sb.append( dest);

        System.out.println(sb.toString());
    }

    public static void main(String[] args) {

        moveDisk(10, 10);
        moveDisk(1, 10);
        moveDisk(20, 10);

    }
}

The outcome:

Moving top disk from 10 to 10
   Moving top disk from 1 to 10
      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:

确定