Topic: 用2维数组怎么做这个程序?不用for

  Print this page

1.用2维数组怎么做这个程序?不用for Copy to clipboard
Posted by: aiff
Posted on: 2005-04-26 00:24

*****
***
* 这个程序用for 是这样写的
class Key
{
  public static void main(String []args)
  {
int size=5;
int hf=size/2;
int i,j;
for(i=0;i<=hf;i++)
{
for(j=0;j<i;j++)
System.out.print(" ");
for(j=0;j<5-i*2;j++)
System.out.print("*");
System.out.print("\n");
}
}
} 还有什么好的方法吗?救命啊..............

2.Re:有没有更好的方法呀?不用for [Re: aiff] Copy to clipboard
Posted by: why
Posted on: 2005-04-26 03:25

What does 更好的方法 mean?
What are you looking for?
我們不是閣下肚裡的迴虫.Sad

anyway, I think it should be
for(j=0; j < size - i*2; j++)

and I may use StringBuffer and one println instead of so many println calls.

3.Re:有没有更好的方法呀?不用for [Re: aiff] Copy to clipboard
Posted by: aiff
Posted on: 2005-04-26 13:22

class qq
{
public static void main(String args[])
  {
    for(int i=1;i>5;i++)

  System.out.print("*");
  for (int j=5;j=2*i-1;j--)
  System.out.print("");

  System.out.println(" ");
}
  } 看看这个怎么错了 我的i已经声明了 老提示我的i

4.Re:有没有更好的方法呀?不用for [Re: aiff] Copy to clipboard
Posted by: shoulders
Posted on: 2005-04-26 18:14

这样就没错了
class qq
{
public static void main(String args[])
{
int i = 0;
for( i=1;i>5;i++)

System.out.print("*");
for (int j=5;j==2*i-1;j--)
System.out.print("");

System.out.println(" ");
}
}

5.Re:有没有更好的方法呀?不用for [Re: aiff] Copy to clipboard
Posted by: why
Posted on: 2005-04-26 19:25

aiff:

Please tell us clearly what you are trying to do,
or this topic will be removed.

6.Re:有没有更好的方法呀?不用for [Re: aiff] Copy to clipboard
Posted by: aiff
Posted on: 2005-04-26 20:42

i want to try the best waySmile
所以我拿了很多的例子做比较 谢谢你一直以来都帮我

7.Re:有没有更好的方法呀?不用for [Re: aiff] Copy to clipboard
Posted by: why
Posted on: 2005-04-26 22:34

aiff wrote:
i want to try the best waySmile
所以我拿了很多的例子做比较

I really don't know what you are trying to do?
Printing an inverted triangle?
What do you want to get better?Question
Better in terms of what??Question

Please read 加贴须知 carefully!
In particular, the second post about 提高发问技巧.
http://www.cjsdn.net/post/view?bid=1&id=119201

8.Re:有没有更好的方法呀?不用for [Re: aiff] Copy to clipboard
Posted by: aiff
Posted on: 2005-04-26 22:44

哦 我忘记补充了 听说这样的程序有人可以用22维数组表示出来 我不知道怎么做 所以继续请教!
附带 此程序的另一中解法

public static void drawtree(int x, int y) {
if(x<=0)return;
for (int i = 0; i < y; i++)
System.out.print(' ');
for (int i = 0; i < x; i++)
System.out.print('*');

System.out.println();
drawtree(x - 2, y +1);
}

public static void main(String[] args)
{
Tree.drawtree(5, 0);
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923