我的工作代码:
公共类SimpleStackDemo {
public static void main(String[] args) {
SimpleStack sT1 = new SimpleStack(10);
char ch;
System.out.println("Outputs:" + "\rDemonstrate SimpleStack" + "\n");
System.out.println("Push 10 items onto a 10-element stack." + "\rPushing: ");
for(ch = 'A'; ch < 'K'; ch++) {
sT1.push(ch);
System.out.print(ch);
给我这个输出:
Outputs:
Demonstrate SimpleStack
Push 10 items onto a 10-element stack.
Pushing:
ABCDEFGHIJ
但我希望它能给我这个输出:
Outputs:
Demonstrate SimpleStack
Push 10 items onto a 10-element stack.
Pushing: ABCDEFGHIJ
我无法弄清楚如何将A-J放在Pushing前面:。我已经尝试\b
,我已经尝试将它放在循环中并且在循环之前,我将获得Pushing: A
Pushing: B
等或其他迭代,其中推送多次抛出。
这是一个简单的事情,我没有看到哪些不需要我做一个全新的编码块?
答案 0 :(得分:3)
为什么不使用System.out.print()
作为标签:
System.out.println("Push 10 items onto a 10-element stack.");
System.out.print("Pushing: ");
for (ch = 'A'; ch < 'K'; ch++) {
sT1.push(ch);
System.out.print(ch);
}
请注意,我删除了您用作换行符的\r
,而是使用了System.out.println()
。这将让Java使用适合您系统的行分隔符。