英文:
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 = " ";
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);
}
}
The outcome:
Moving top disk from 10 to 10
Moving top disk from 1 to 10
Moving top disk from 20 to 10
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论