求:用Java语言写只用一个for循环输出“*”组成的空心菱形的思路或代码
2个回答
展开全部
public class Kong{
public static void main(String []args){
int hangshu = 11;
int yiban = hangshu / 2 + 1;
int yibanduo = hangshu / 2;
for(int k = 1;k <= yiban;k++){
for(int i = 1;i <= (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i <= ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
for(int k = yibanduo;k >=1;k--){
for(int i = 1;i <= (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i <= ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
}
}
public static void main(String []args){
int hangshu = 11;
int yiban = hangshu / 2 + 1;
int yibanduo = hangshu / 2;
for(int k = 1;k <= yiban;k++){
for(int i = 1;i <= (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i <= ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
for(int k = yibanduo;k >=1;k--){
for(int i = 1;i <= (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i <= ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Fun {
public int len=12;//菱形边长
public void dosth(){
for(int i=0;i<(2*len-1)*len;i++){
if(i/(2*len-1)+i%(2*len-1)==len||i/(2*len-1)+i%(2*len-1)==2*len-1||(i>=len&&i<=2*len-1)||(i>=(2*len-1)*(len-1)+1&&i<=(2*len-1)*len-len+1)){
System.out.print("*");
}else{
System.out.print(" ");
}
if(i%(2*len-1)==0){
System.out.println();
}
}
}
public static void main(String[] args){
Fun f=new Fun();
f.dosth();
}
}
嘿嘿,挺费劲啊
哦,对了
你是要
***
* *
***这样的?
还是
*
* *
* *
* *
*这样的?
public int len=12;//菱形边长
public void dosth(){
for(int i=0;i<(2*len-1)*len;i++){
if(i/(2*len-1)+i%(2*len-1)==len||i/(2*len-1)+i%(2*len-1)==2*len-1||(i>=len&&i<=2*len-1)||(i>=(2*len-1)*(len-1)+1&&i<=(2*len-1)*len-len+1)){
System.out.print("*");
}else{
System.out.print(" ");
}
if(i%(2*len-1)==0){
System.out.println();
}
}
}
public static void main(String[] args){
Fun f=new Fun();
f.dosth();
}
}
嘿嘿,挺费劲啊
哦,对了
你是要
***
* *
***这样的?
还是
*
* *
* *
* *
*这样的?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询