每个循环减少字符串中的空格

时间:2017-05-14 10:03:46

标签: java

我试图在同一行打印出2个用*做的形状。 最终结果必须如下所示

//  * * * * *             *  
//  * * * * *            * *  
//  * * * * *           * * *
//  * * * * *          * * * *

到目前为止我的代码工作,但我不确定如何减少每个循环的形状之间的空间。

当前代码:

String rectangleLine = "* * * * *";
int x = 1;
    for(int a = 0; a < 4; a++){
    String repeated = new String(new char[x]).replace("\0", "* ");        
    String line = rectangleLine + "             " + repeated; 
    System.out.println(line);
    x++;
}

目前的结果是:

* * * * *             * 
* * * * *             * * 
* * * * *             * * * 
* * * * *             * * * * 

我如何减少每个循环的空间?

1 个答案:

答案 0 :(得分:0)

试试这个

class A
{
protected:
double x,y,z;
Function() {
*do something using the member variables of class A and the member functions of class A* }

private:
double PrivFunction() {
*take in member variables from A and return a certain value* }

double PrivFunction2() {
*take in member variables from A and return a certain value* }

class B : public A
{
private:
double a,b,c;

double PrivFunction() {
*take in member variables from A,B and return a certain value* }

double PrivFunction2() {
*take in member variables from A,B and return a certain value* }

main() {
B classb();
B.Function() 
}