Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法在cmd窗口中自动换行?_Java - Fatal编程技术网

Java 有没有办法在cmd窗口中自动换行?

Java 有没有办法在cmd窗口中自动换行?,java,Java,我和我的朋友写了一个简单(但很长)的基于文本的游戏,在Windows命令提示符下玩。问题是,cmd窗口没有像Microsoft word中那样包含任何类型的自动换行,因此使游戏无法播放。如果我不是很清楚的话,这是一个关于游戏如何玩的例子: You wake up in a dark room. Wherev er you look, you can't see anythin g. You try to move, but you can't; your hands are tied down

我和我的朋友写了一个简单(但很长)的基于文本的游戏,在Windows命令提示符下玩。问题是,cmd窗口没有像Microsoft word中那样包含任何类型的自动换行,因此使游戏无法播放。如果我不是很清楚的话,这是一个关于游戏如何玩的例子:

You wake up in a dark room. Wherev
er you look, you can't see anythin
g. You try to move, but you can't;
your hands are tied down by the lo
oks of it.
有什么简单的方法可以解决这个问题吗?这个游戏有2000多行,因此我们可能永远无法一个接一个地修正句子

编辑:只是想说明一下,我想实现以下目标:

You wake up in a dark room.
Wherever you look, you can't see
anything. You try to move, but
you can't; your hands are tied
down by the looks of it.

这并不是一个实际的java问题,正如标签所建议的,但是如果您使用System.out作为输出, 你可以:

  • 更改windows cmd宽度以满足您的需要
  • 根据链接,动态获取cmd宽度。 然后,创建一个类的对象,它将是您的输出处理程序。获取宽度并将其另存为字段。然后,无论何时需要输出,都可以调用它的方法,该方法会自动将换行符放到字符串中,然后调用redirect to system.out

  • 一个非常简单的例子。在你的情况下,你可能需要存储你的输入,直到换行的地方进来,等等。但是我希望你能得到基本的想法

    public class RedirectPrintStream extends PrintStream {
    
        private static String NEW_LINE = String.format("%n"); // This creates the system-specific line break (depends on Windows/Linux/etc)
    
        public RedirectPrintStream(OutputStream out) {
            super(out); 
        }
    
        @Override
        public void print(String obj) {
            super.print(obj);  // or check here if the obj.length > 80 and add another line break, etc.
            super.print(NEW_LINE);
    
        }
    
        public static void main(String[] args) {
    
            PrintStream old = System.out;
            System.setOut( new RedirectPrintStream(old));
    
            System.out.print("Hello");
            System.out.print("World"); // will be printed in a new line instead of the same
    
        }
    }
    

    至于包装本身,我建议使用Apache Commons lang中已经提到的第二个包装。应该足够简单。

    通常,宽度为72的行用于使文本易于阅读,因此我建议坚持这一点,不要尝试计算文本终端的宽度。要实现正确的单词包装,最好的选择是依赖ApacheWordUtils,它包含该方法。或者(如果您不想为此下载另一个JAR),只需将其实现复制到您的项目中:

    /*
     * Copyright 2002-2005 The Apache Software Foundation.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {
        if (str == null) {
            return null;
        }
        if (newLineStr == null) {
            newLineStr = SystemUtils.LINE_SEPARATOR;
        }
        if (wrapLength < 1) {
            wrapLength = 1;
        }
        int inputLineLength = str.length();
        int offset = 0;
        StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);
    
        while ((inputLineLength - offset) > wrapLength) {
            if (str.charAt(offset) == ' ') {
                offset++;
                continue;
            }
            int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);
    
            if (spaceToWrapAt >= offset) {
                // normal case
                wrappedLine.append(str.substring(offset, spaceToWrapAt));
                wrappedLine.append(newLineStr);
                offset = spaceToWrapAt + 1;
    
            } else {
                // really long word or URL
                if (wrapLongWords) {
                    // wrap really long word one line at a time
                    wrappedLine.append(str.substring(offset, wrapLength + offset));
                    wrappedLine.append(newLineStr);
                    offset += wrapLength;
                } else {
                    // do not wrap really long word, just extend beyond limit
                    spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
                    if (spaceToWrapAt >= 0) {
                        wrappedLine.append(str.substring(offset, spaceToWrapAt));
                        wrappedLine.append(newLineStr);
                        offset = spaceToWrapAt + 1;
                    } else {
                        wrappedLine.append(str.substring(offset));
                        offset = inputLineLength;
                    }
                }
            }
        }
    
        // Whatever is left in line is short enough to just pass through
        wrappedLine.append(str.substring(offset));
    
        return wrappedLine.toString();
    }
    
    /*
    * Apache软件基金会版权2002年至2005年。
    *
    *根据Apache许可证2.0版(以下简称“许可证”)获得许可;
    *除非遵守许可证,否则不得使用此文件。
    *您可以通过以下方式获得许可证副本:
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
    *除非适用法律要求或书面同意,软件
    *根据许可证进行的分发是按“原样”进行分发的,
    *无任何明示或暗示的保证或条件。
    *请参阅许可证以了解管理权限和权限的特定语言
    *许可证下的限制。
    */
    公共静态字符串换行(String str、int wrapLength、String newLineStr、boolean wrapLongWords){
    如果(str==null){
    返回null;
    }
    如果(newLineStr==null){
    newLineStr=SystemUtils.LINE\u分隔符;
    }
    如果(宽度<1){
    wrapLength=1;
    }
    int inputLineLength=str.length();
    整数偏移=0;
    StringBuilder wrappedLine=新StringBuilder(inputLineLength+32);
    while((inputLineLength-偏移量)>wrapLength){
    如果(字符串(偏移量)=''){
    offset++;
    继续;
    }
    int spacetowrappat=str.lastIndexOf(“”,wrapLength+偏移量);
    如果(空格拖距>=偏移量){
    //正常情况
    append(str.substring(offset,spacetowrappat));
    wrappedLine.append(newLineStr);
    偏移量=间距拖距+1;
    }否则{
    //非常长的单词或URL
    如果(纠缠词){
    //把很长的单词一行一行地包装起来
    append(str.substring(offset,wrappedLine+offset));
    wrappedLine.append(newLineStr);
    偏移量+=wrapLength;
    }否则{
    //不要把真的很长的字包起来,只要超出限制就行了
    spaceToWrapAt=str.indexOf(“”,wrapLength+偏移量);
    如果(空格拖距>=0){
    append(str.substring(offset,spacetowrappat));
    wrappedLine.append(newLineStr);
    偏移量=间距拖距+1;
    }否则{
    wrappedLine.append(str.substring(offset));
    偏移量=输入线长度;
    }
    }
    }
    }
    //剩下的东西都足够短,可以直接穿过
    wrappedLine.append(str.substring(offset));
    返回wrappedLine.toString();
    }
    
    我不确定您想做什么,但如果所有游戏都在写入System.out,那么您可以使用System.setOut(…)将其设置为您自己的打印流,该打印流将其重定向到System.out,只添加换行符。您将如何做到这一点?网上有什么教程吗?我是一个Java初学者。这取决于你的换行标准是什么…基本上cmd窗口每行有80个字符的限制,然后在下一行继续打印文本。我的标准如下:在每80个字符中,它查找最后一个空格,并打印出新行。这可能实现吗?在我看来似乎很难实现,因为您没有命令提示窗口的属性。(字体、文本尺寸、窗口尺寸)这听起来比实际从一些教程中获取文本区域的示例代码并重定向系统要麻烦得多。只要做一点工作,您甚至可以让它看起来像命令行提示符(如果您愿意)。该链接说明可以使用jline2,但实际上它有一个名为
    jansi
    的可传递依赖项,并使用其内部API(在Github的当前版本中删除)。Jansi要求系统上有一个本机DLL。据我所知,OP最大的问题是,在哪里断线,而不是如何断线。这可能会有所帮助,但找出每行有多少个字符对于这项工作通常是至关重要的。这不是完整的答案。好的,添加了一些内容。我个人也会使用ApacheCommonsLang来实现这一点。不需要从头开始写。