2017-06-06 49 views
0

我的工作代码:把一份声明中一次循环之前

公共类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在推动前面朝上:。我试过\b,我试过把它放在循环中,然后在循环之前,我会得到Pushing: APushing: B等等或其他迭代,在这种迭代中抛出多次。

这是一个简单的事情,我没有看到哪些不需要我做一个全新的编码块?

+0

假设您在一个单独的行中使用'System.out.print(“Pushing:”);''。 –

回答

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使用适合您的系统的行分隔符。

+0

OH。 MY。神。认真!我是一个朴实的人。当然,我没有想到......哇。 – Abraxas