(Java)我正在用for循环制作水平尺

时间:2019-02-26 02:42:06

标签: java

我正尝试创建一个水平标尺,如下所示:

我现在拥有的代码是:

public class JavaRuler {
    public static final int COUNT = 19;

    public static void main(String[] args) {
        Design();
        Inches();
        Number();
    }

    public static void Design() {

        for (int j = 1; j <= COUNT * 2 + 2; j++) {
            System.out.print("__");
        }
        System.out.println();
    }

    public static void Inches() {

        for (int i = 1; i <= COUNT * 2 + 2; i++) {
            System.out.print("'|");
        }
        System.out.println();
    }

    public static void Number() {
        for (int k = 1; k <= COUNT; k++) {
            System.out.print("___" + k);
        }   
    }
}

标尺必须以英寸为单位调整大小,如果不允许使用语句(仅用于循环),我的当前输出确实很接近,但是我似乎无法弄清楚如何减小第三行中的“ _”数字超过9。有什么提示吗?

3 个答案:

答案 0 :(得分:1)

我认为我的示例可以解决问题

public static void Number() {
    for (int k = 1; k <= COUNT; k++) {
      System.out.print(("____" + k).substring(("" + k).length()));
    }
}

结果

'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|
___1___2___3___4___5___6___7___8___9__10__11__12__13__14__15__16__17__18__19

答案 1 :(得分:1)

您可以参考此内容,但是如果COUNT是一个很大的数字,结果将是一团糟。我还更新了您的一些代码,以使inchesdesign不会与最后一个数字重叠。

public class JavaRuler {
    public static final int COUNT = 20;

    public static void main(String[] args) {
        Design(" ");
        Inches();
        Number();
        Design("|");
    }

    public static void Design(String s) { // Added parameter
        System.out.print(s + "_"); // Added
        for (int j = 0; j < COUNT * 2; j++) { // Updated
            System.out.print("__");
        }
        System.out.print(s); // Added
        System.out.println();
    }

    public static void Inches() {
        System.out.print("|"); // Added
        for (int i = 0; i < COUNT * 2; i++) { // Updated
            System.out.print("'|");
        }
        System.out.print("'|"); // Added
        System.out.println();
    }

    public static void Number() {
        System.out.print("|"); // Added
        for (int k = 1; k <= COUNT; k++) {
            String underScore = "   "; // Added
            int length = (int) (Math.log10(k - 1) + 1); 
            for (int i = 1; i < length; i++) {
                underScore = underScore.replaceFirst(" ", ""); // Added
            }
            System.out.print(underScore + k); // Updated
        }
        System.out.print("|"); // Added
        System.out.println();

    }
}

结果:

 _________________________________________________________________________________ 
|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|
|   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20|
|_________________________________________________________________________________|

答案 2 :(得分:0)

今天晚些时候我回到了它,它打了我!我想通了!

public class Ruler {
public static final int COUNT = 19;

public static void main(String[] args) {
    Design();
    Inches();
    Numbers();
}

public static void Design() {

    for (int j = 1; j <= COUNT * 2 + 2; j++) {
        System.out.print("__");
    }
    System.out.println();
}

public static void Inches() {
    System.out.print("|");
    for (int i = 1; i <= COUNT * 2 + 2; i++) {
        System.out.print("'|");
    }
    System.out.println();
    System.out.print("");
}

public static void Numbers() {
    System.out.print("|");
    for (int k = 1; k <= 9; k++) {
        System.out.print("___" + k);
    }
    System.out.print("___");
    for (int m = 10; m <= COUNT; m++){
        System.out.print(m + "__");
        }
    System.out.print("|");
    }
}