黑马程序员技术交流社区
标题:
关于用循环在控制台输出菱形的*,并且要居中显示
[打印本页]
作者:
左星辰
时间:
2016-5-29 17:50
标题:
关于用循环在控制台输出菱形的*,并且要居中显示
有没有什么很简单的思路啊?想了好久都办不到,关键居中难以实现
作者:
yuxing
时间:
2016-5-29 18:28
这个你把它画出来,然后用if(i,j)即行列的方式直接输出*就行了。。
作者:
TS-松子
时间:
2016-5-29 22:35
只会玩菱形,如果知道打印台的长度的话就可以居中吧,,没这么玩过。
作者:
nanliner
时间:
2016-5-30 00:19
/*输出菱形星形
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*/
/*class LingXing {
public static void main(String[] args) {
for(int x=1;x<=13;x++){
for(int m=0;m<=7-x;m++){
System.out.print(" ");
}
if(x>7){
for(int n=0;n<=x-7;n++){
System.out.print(" ");
}
}
for(int y=1;y<=x&&y<=14-x;y++){
System.out.print("*"+" ");
}
System.out.println();
}
}
}*/
键盘录入数据
import java.util.Scanner;
class LingXing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据:");
int a = sc.nextInt();
if(a%2==0){
for(int x=1;x<=a;x++){
for(int m=0;m<=(a)/2-x;m++){
System.out.print(" ");
}
if(x>(a)/2){
for(int n=0;n<=x-((a)/2);n++){
System.out.print(" ");
}
}
for(int y=1;y<=x&&y<=(a)-x;y++){
System.out.print("*"+" ");
}
System.out.println();
}
}else{
for(int x=1;x<=a;x++){
for(int m=0;m<=(a+1)/2-x;m++){
System.out.print(" ");
}
if(x>(a+1)/2){
for(int n=0;n<=x-((a+1)/2);n++){
System.out.print(" ");
}
}
for(int y=1;y<=x&&y<=(a+1)-x;y++){
System.out.print("*"+" ");
}
System.out.println();
}
}
}
}
复制代码
作者:
1985206835long
时间:
2016-5-30 09:20
很好很好很好
作者:
SkyBlack
时间:
2016-5-30 09:44
这个菱形不是很标准啊
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2