JAVA打印一个倒立和正立的金字塔,就像影子那样的,原来会,突然想不起来了,谢谢先,详细代码啊
2013-12-01
展开全部
import java.util.*;
public class lingxing {
public static void main(String args[]){
int hangshu=1;
int zhongjianhang;
int i;
int kongge;
int xing;
Scanner shuru=new Scanner(System.in);
System.out.println("请输入行数(必须是奇数!):");
hangshu=shuru.nextInt();
while(hangshu%2==0){
System.out.println("输入有误,行数必须是奇数!):");
System.out.println("请输入行数(必须是奇数!):");
hangshu=shuru.nextInt();
}
zhongjianhang=(hangshu+1)/2;
for(i=0;i<=hangshu;i++){
if(i==zhongjianhang){
for(xing=0;xing<hangshu;xing++){
System.out.print("*");
}
System.out.println();
}
else if(i<zhongjianhang){
for(kongge=0;kongge<zhongjianhang-i;kongge++){
System.out.print(" ");
}
for(xing=0;xing<i*2-1;xing++){
System.out.print("*");
}
System.out.println();
}
else{
for(kongge=0;kongge<i-zhongjianhang;kongge++){
System.out.print(" ");
}
for(xing=0;xing<hangshu-(i-zhongjianhang)*2;xing++){
System.out.print("*");
}
System.out.println();
}
}
}
}不会可追问
public class lingxing {
public static void main(String args[]){
int hangshu=1;
int zhongjianhang;
int i;
int kongge;
int xing;
Scanner shuru=new Scanner(System.in);
System.out.println("请输入行数(必须是奇数!):");
hangshu=shuru.nextInt();
while(hangshu%2==0){
System.out.println("输入有误,行数必须是奇数!):");
System.out.println("请输入行数(必须是奇数!):");
hangshu=shuru.nextInt();
}
zhongjianhang=(hangshu+1)/2;
for(i=0;i<=hangshu;i++){
if(i==zhongjianhang){
for(xing=0;xing<hangshu;xing++){
System.out.print("*");
}
System.out.println();
}
else if(i<zhongjianhang){
for(kongge=0;kongge<zhongjianhang-i;kongge++){
System.out.print(" ");
}
for(xing=0;xing<i*2-1;xing++){
System.out.print("*");
}
System.out.println();
}
else{
for(kongge=0;kongge<i-zhongjianhang;kongge++){
System.out.print(" ");
}
for(xing=0;xing<hangshu-(i-zhongjianhang)*2;xing++){
System.out.print("*");
}
System.out.println();
}
}
}
}不会可追问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询