【问题标题】:Terrain drawing Java地形绘制Java
【发布时间】:2020-01-19 22:57:22
【问题描述】:

我想做一个程序来产生这样的东西(下划线上下颠倒):output1

注意:我不想使用 UNICODE。

但相反,我的输出是这样的(下划线打印在底部):output2

public class Landscape {

    String terrainString;

    Landscape(){
        terrainString = "";
    }

    public void flat(int lengthOfFlatPortion){
        for (int count = 0; count < lengthOfFlatPortion; count++) {
            terrainString += "_";
        }
    }

    public void hill(int lengthOfHillTop){
        terrainString += "/";
        for (int count = 0; count < lengthOfHillTop; count++) {
            terrainString += "_";
        }
        terrainString += "\\";

    }

    public void print(){
        System.out.println(terrainString);
    }
}

public class Main {
    public static void main(String[] args){
        Landscape landscape = new Landscape();
        //BUILD Landscape Script
        landscape.flat(3);
        landscape.hill(4);
        landscape.flat(6);
        landscape.hill(1);
        landscape.flat(1);
        //END SCRIPT
        landscape.print();
    }
}

【问题讨论】:

标签: java class oop


【解决方案1】:

使用 字符而不是_

public void hill(int lengthOfHillTop){
    terrainString += "/";
    for (int count = 0; count < lengthOfHillTop; count++) {
        terrainString += "‾";
    }
    terrainString += "\\";

}

【讨论】:

    【解决方案2】:

    正如我在 cmets 中已经说过的,在方法 hill(...) 中使用 (unicode 203e) 而不是 _

    要在 java 中打印 unicode 字符,请在其前面加上 \u。所以要打印上划线,请使用字符串"\u203e"

    如果不想使用 unicode 字符,则必须在第一行上方打印第二行。这意味着该 hill 线必须在打印其余部分之前打印。该行应仅包含空格和下划线,并且下划线应仅在平坦的山体表面处打印。

    Ideone demo

    【讨论】:

    • 谢谢,但我想避免使用 unicode 并可能合并第二个变量(即,terrainString2),有什么建议吗?
    • ...您应该在问题中如此说明。是的,我有。
    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 2014-11-18
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    相关资源
    最近更新 更多