Java怎么用for循环打星号"*",打出一个空心菱形?
发布网友
发布时间:2024-05-05 21:07
我来回答
共4个回答
热心网友
时间:2024-07-13 04:10
看到过此题,本人还是初级javaa水平,解决不了。共同进步啊!!!!!!!!
热心网友
时间:2024-07-13 04:10
我给你写了一个,参考一下吧。。import java.util.*;
public class H
{
public static void main(String args[])
{
int a,b,c,N;
System.out.println("请输入菱形的边长N");
N=new Scanner(System.in).nextInt();
for(a=0;a<N;a++)
{
for(b=0;b<=N-a;b++)
System.out.print(" ");
for(c=1;c<=2*a-1;c++)
{
if(c==1||c==2*a-1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.print("\n");
}
for(a=0;a<=N;a++)
{
for(b=0;b<=a;b++)
System.out.print(" ");
for(c=1;c<=2*(N-a)-1;c++)
if(c==1||c==2*(N-a)-1)
System.out.print("*");
else
System.out.print(" ");
System.out.print("\n");
}
}
}
热心网友
时间:2024-07-13 04:13
public class Test {
public static void print(int n){
int index1 = n/2;
int index2 = index1;
for (int i = 0; i < n; i++){
for (int j = 0; j < 2 * n - 1; j++){
if (j == index1 || j == index2){
System.out.print("*");
} else{
System.out.print(" ");
}
}
if ( i >= n / 2){
index1 += 2;
index2 -= 2;
} else{
index1 -= 2;
index2 += 2;
}
System.out.println();
}
}
public static void main(String args[]){
print(9);
}
}
热心网友
时间:2024-07-13 04:09
package com.ga.isl.test;
public class CubeTest { public static void main(String[] args) { for(int i = 0;i<3;i++){ for(int j=0;j<3;j++){ if( 1 == (i+j)%2){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } }}