For和while循环来获得这种装饰 [英] For and while loop to get this decoration

查看:85
本文介绍了For和while循环来获得这种装饰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个结果:



-

-

-

-

*

*

*

*

*



但我希望看到:



---- *

- - **

- ***

- ****

*****



我尝试过:



包演示;



公共类酯类{



public static void main(String [] args){





// ---- * ////// x = - / 4 /

// --- ** ///// y = * / 5 /

// - ***

// - ****

// *****

for(int i = 1; i< 5;){

System.out.println( - );

i ++;

}

for(int y = 5; y> = 1;){

System.out.println('*');

y - ;

}

}

}

I got this result:

-
-
-
-
*
*
*
*
*

but I want to see:

----*
---**
--***
-****
*****

What I have tried:

package demo;

public class esterics {

public static void main(String[] args) {


//----* ////// x = - /4/
//---** ///// y = * /5/
//--***
//-****
//*****
for (int i = 1;i<5;){
System.out.println("-");
i++;
}
for (int y = 5; y>=1;){
System.out.println('*');
y --;
}
}
}

推荐答案

首先不使用 println 一直以来。每次调用它时,它会在输出上创建一个新行,您只需要在每行之后,而不是每个字符。

使用 System.out.print 打印行中的每个字符,然后 println 完成准备下一行的行。



并且......你将不得不嵌套这些循环

Start by not using println all the time. Each time you call it, it creates a new line on the output, and you only need that after each row, not each character.
Use System.out.print to print each individual character in the row, then println to finish the row ready for the next one.

And ... you are going to have to "nest" those loops
for (int y = ...  // for each row
   for (int x = ... // for each character in the row
      // do the character
   }
   // use println to end the row ready for the next one
   ...
}


这篇关于For和while循环来获得这种装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆