编写打印如下的*号,帮帮忙了,看看谁写的即简单又实用
*******
*****
***
*
***
*****
*******
------解决方案--------------------
System.out.println( "******* ");
System.out.println( " ***** ");
System.out.println( " *** ");
System.out.println( " * ");
System.out.println( " *** ");
System.out.println( " ***** ");
System.out.println( "******* ");
符合简单实用的标准不?
------解决方案--------------------
无聊的小顶一下
public class Test9 {
public static void main(String[] args) {
int n = 20;
int width = 2 * n - 1;
char[] c = new char[width];
for(int i=0; i <width; i++)
c[i] = '* ';
int big = width -1;
int small = 0;
for(int i=0; i <n-1; i++) {
System.out.println(c);
c[big] = ' '; big--;
c[small] = ' '; small++;
}
for(int i=n; i <2*n-1; i++) {
System.out.println(c);
big++; c[big] = '* ';
small--; c[small] = '* ';
}
System.out.println(c);
}
}
------解决方案--------------------
又是这种题呢
for(int i=1,len=7;i <=len;i++)
for(int j=1;j <=len;j++)
System.out.print((Math.abs(j-len/2-1) <=Math.abs(len/2-i+1)? "* ": " ")+(j==len? "\n ": " "));
------解决方案--------------------
class Test
{
public static void main(String[] args)
{
print(8,2, '* ');
}
public static void print(int start,int step,char ch){
if(start <=0||step <=0)return;
int i=0;
for(i=start;i> 0;i-=step){
for(int j=0;j <i;j++)
System.out.print(ch);
System.out.println();
}
for(i+=2*step;i <=start;i+=step){
for(int j=0;j <i;j++)
System.out.print(ch);
System.out.println();
}
}
}
------解决方案--------------------
public static void main(String[] arg) {
final int LINE = 7;
// 算法1
for (int i = -LINE / 2; i <= LINE / 2; i++) {
for (int j = 1; j <= 2 * Math.abs(i) + 1; j++) {
System.out.print( "* ");
}
System.out.println();
}
System.out.println();
// 算法2:这个比较有意思
int tmp = (LINE / 2 + 1) * (LINE / 2 + 1);
for(int i = -tmp + 1 ; i <= tmp; i++ ){
if(i == 1) {
continue;
}
System.out.printf( "%c%s ", '* ', (Math.sqrt(Math.abs(i)) % 1 == 0) ? "\n " : " ");
}
}
------解决方案--------------------
char[] c = new char[7];//数组的长度是第一行的星星的个数
for(int i = 0;i <c.length;i++)
{
c[i] = '* ';
}
int first = 0;
int last = c.length-1;
for(int i = 7;i> 0;i--)
{
if(first ==0)
System.out.println(c);
c[first] = ' ';
c[last] = ' ';
first++;
last --;
System.out.println(c);
if(first == last)
{
while( first != c.length && last != 0)
{
c[++first] = '* ';