简体   繁体   English

Eclipse控制台输出格式问题

[英]Eclipse console output formatting issue

I have set the console to have a fixed width and I have also increased the text size of the console output. 我将控制台设置为固定宽度,并且还增加了控制台输出的文本大小。 I have a custom method which takes each character of a string and displays it in System.out , one character at a time with a Thread.sleep in between each character output. 我有一个自定义方法,该方法接受字符串的每个字符并将其显示在System.out ,一次显示一个字符,并且在每个字符输出之间插入Thread.sleep
It was working fine until I changed the size of the text, but changing it back does not work. 直到我更改了文本大小,它都可以正常工作,但是将其改回是行不通的。 Basically once it hits the max width, it doesn't continue printing the characters, some of them do print out but most of them do not. 基本上,一旦达到最大宽度,它就不会继续打印字符,其中一些字符会打印出来,但大多数字符不会打印出来。 However if I change the font size after it has run, all the text appears the way it should, but I can never get it to appear this way on the first run of the application. 但是,如果我在运行后更改字体大小,则所有文本都会以应有的方式显示,但我永远无法在应用程序的第一次运行中以这种方式显示它。 I hope I am explaining this well enough. 我希望我已经解释得足够好了。 Please let me know if you would like any more information. 如果您需要更多信息,请告诉我。 Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。
I am not sure how to edit my comment to post the code correctly so i'll add it in here here is the code for the method: 我不确定如何编辑我的评论以正确发布代码,因此我将在此处添加该方法的代码:

public static void delayedPrint(int delay, String s){
        try {
            for (char c : s.toCharArray()){
                System.out.print(c);
                Thread.sleep(delay);
            }
        }catch (InterruptedException e){
        }
        System.out.println();
    }

I implement it here: 我在这里实现它:

    delayedPrint(40,"You wake. You remember nothing. Nothing is familiar: not the smell, which hangs in the air like the inside of a Lysol bottle, nor the taste—yes the room has a taste…like a college dining hall cleaned with dirty rags. And not the sight: This is a hospital cot, isn’t it?");

This works fine until I turn on fixed width in the console settings, that's when the output becomes wonky when it reaches the max width. 在我在控制台设置中打开固定宽度之前,这是可以正常工作的,也就是说,当输出达到最大宽度时,该输出将变得不稳定。

I am not sure how much of an answer this is 我不确定这是多少答案

Something similar is happening when i try it as well. 当我也尝试类似的事情发生。 I am quite sure its an Eclipse thing though, because if you run it on cmd, it works just fine. 我相当确定它是Eclipse的东西,因为如果您在cmd上运行它,则效果很好。 I found a way around it that prints all characters fine for me. 我找到了解决该问题的方法,该方法可以很好地打印所有字符。

So, assuming you have a fixed width of 80 characters lets say. 因此,假设您的固定宽度为80个字符,可以这样说。 Then you can do the following: 然后,您可以执行以下操作:

public static void delayedPrint(int delay, String s){
        int charCounter = 0;
        try {
            for (char c : s.toCharArray()){     
                System.out.print(c);
                charCounter++;
                if(charCounter % 80 == 0) System.out.println();//Replace 80 with your console width
                Thread.sleep(delay);
            }
        }catch (InterruptedException e){
        }
        System.out.println();
    }

So basically force a linebreak every time you reach the console width. 因此,基本上每次您到达控制台宽度时都强制换行。 I am not sure how useful it is for you. 我不确定它对您有多有用。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM