黑马程序员技术交流社区
标题:
输出菱形星星图案
[打印本页]
作者:
nanliner
时间:
2016-5-30 00:22
标题:
输出菱形星星图案
输出菱形星形
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
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();
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2