【问题标题】:Trouble centering text output文本输出居中问题
【发布时间】:2020-02-02 04:51:49
【问题描述】:

所以,我有一个任务要求我根据用户输入打印一些形状(树)。我得到了正确的所有形状,但还有第二个形状也应该打印,一个底部正方形(树的底部),第二个正方形应该与顶部正方形(实际的树)居中。

这是目前为止的代码:

public class TreeStructures {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        int height = 0;
        int weidth = 0;
        int weidth2 = 0;

        //Introduction
        System.out.println("Tree Structures");
        System.out.println("---------------\n");
        System.out.println("This program prints two tree like structures! ");
        System.out.println("A \"Flat\" tree and a \"Christmas\" tree.");
        System.out.println("You choose how big the trees will be.\n");
        System.out.println("By Daniel Sousa (A00433806)\n");

        //User input
        System.out.print("How tall should the top of the tree be? ");
        height = scnr.nextInt();
        System.out.println();

        //Input check
        if (height > 4 && height < 21) {            
            System.out.println("Flat Tree:");

            //Top Square
            for (int i = 1; i <= height; i++) {
                System.out.println(" ");
                for (int j = 0; j <= (height*2) - 2; j++) {
                    System.out.print("*");
                    weidth = j;
                }
            }
            //Bottom Square
            for (int i = 1; i <= (height/5) + 1; i++) {
                System.out.println(" ");
                for (int k = 1; k <= (weidth - weidth2)/2; k++) {
                System.out.print(" ");
                }
                for (int j = 0; j <= weidth/3; j++) {
                    System.out.print("<");
                    weidth2 = j;
                }

            }
        }

        //Invalid Input
        else {
            System.out.println("That's not a valid size.  "
                    + "I can only do trees from 5 to 20.\n");
            System.out.println("Quitting now.\n");
            System.out.print("Press enter...");
            scnr.nextLine();
            scnr.nextLine();
        }
    }
}
\\\

这是用于创建第二个矩形并将其居中的特定代码块:

for (int i = 1; i <= (height/5) + 1; i++) {
            System.out.println(" ");
            for (int k = 1; k <= (weidth - weidth2)/2; k++) {
            System.out.print(" ");
            }
            for (int j = 0; j <= weidth/3; j++) {
                System.out.print("<");
                weidth2 = j;
            }

        }

作为输出,它按预期创建了第二个矩形,甚至还可以正确缩进,但是缩进的第一行由于我无法解释的原因产生了太多的空格。

输出示例(将“X”替换为空格):

\\\
Tree Structures
---------------

This program prints two tree like structures! 
A "Flat" tree and a "Christmas" tree.
You choose how big the trees will be.

By Daniel Sousa (A00433806)

How tall should the top of the tree be? 19

Flat Tree:

************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
************************************* 
XXXXXXXXXXXXXXXXXX<<<<<<<<<<<<< //!-!-!-! <-problematic line
XXXXXXXXXXXX<<<<<<<<<<<<< 
XXXXXXXXXXXX<<<<<<<<<<<<< 
XXXXXXXXXXXX<<<<<<<<<<<<

我的问题是:如何使第二个矩形缩进(“X”)的第一行与其他行一样多。或者更好的是,如何使第二个矩形与第一个矩形的中间对齐?

谢谢!

【问题讨论】:

    标签: java alignment center


    【解决方案1】:

    您似乎使用 for 循环使其过于复杂,使用 2 个循环(您移动的每个轴 1 个)并简单地决定是否应该打印一些东西不是更容易吗?像这样:

    for (int i = 1; i <= (height/5) + 1; i++) {
        System.out.println(" ");
        for (int k = 1; k <= 2*weidth/3; k++) {
            if (k < weidth/3){
                System.out.print(" ");
            } else {
                System.out.print("<");
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      问题在于您的第一次迭代 weidth2 = 0。

      for (int k = 1; k <= (weidth - weidth2)/2; k++) {
          System.out.print(" ");
      }
      
      for (int j = 0; j <= weidth/3; j++) {
          System.out.print("<");
          weidth2 = j; //happens after first printing of spaces
      }
      

      所以你需要做的是先设置width2。 (我把weidth2改成了width2)

      这个:

      //Bottom Square
      for (int i = 1; i <= (height/5) + 1; i++) {
          System.out.println(" ");
          width2 = width/3;
      
          for (int k = 1; k <= (width - width2)/2; k++)
              System.out.print(" ");
      
          for (int j = 0; j <= width/3; j++)
              System.out.print("<");
      }
      

      产生这个:

      How tall should the top of the tree be? 10
      
      Flat Tree:
      
      ******************* 
      ******************* 
      ******************* 
      ******************* 
      ******************* 
      ******************* 
      ******************* 
      ******************* 
      ******************* 
      ******************* 
            <<<<<<< 
            <<<<<<< 
            <<<<<<<
      

      我不确定作业的要求,但是这棵树是 19 * 宽,看起来这是一个错误,应该是 20 * 宽。但这解决了您的对齐问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-18
        • 2013-10-05
        • 1970-01-01
        • 2018-01-16
        • 1970-01-01
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多