在单行上显示多个变量?

时间:2015-02-04 21:35:15

标签: java stack push is-empty

所以我正在做一个程序,它读取长达80个字符的消息,并使用push,pop和isempty方法将其显示回用户。唯一的问题是,单个变量将打印在该行上,因此向后消息一次在屏幕上垂直向下移动一个字母。代码如下,有人可以告诉我正确的命令或需要修复的内容吗?

public class StackUser

{
static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)
    {
        int length;
        int I = 0;
        char currChar;
        int Max = 80;
        myStack Placeholder;
        Placeholder = new myStack();

        System.out.println("Please enter any word or sentence up to a maximum of 80 characters long.");
        String userEnter = keyboard.nextLine();
        length = userEnter.length();
        if (length < Max)
            {

                while (I < length)
                    {
                        char res = userEnter.charAt(I); 
                        Placeholder.PushChar(res); 
                        I = I + 1;
                    }
            }   
        if (Max < length)
            {

                while (I < Max)
                    {
                        char res = userEnter.charAt(I);
                        Placeholder.PushChar(res);
                        I = I + 1;
                    }
            }
        while (Placeholder.IsEmpty() != true)
            {
                currChar = Placeholder.PopChar();
                System.out.println("Here is your message backwards:" + currChar);
            }

        }
}

1 个答案:

答案 0 :(得分:2)

只需使用print而不是println:

    System.out.print("Here is your message backwards: ");
    while (Placeholder.IsEmpty() != true) {
        currChar = Placeholder.PopChar();
        System.out.print(currChar);
    }
    System.out.println();