大家帮忙给出经典的方法

cyuan09 2010-04-15 04:42:20
今天没事儿做了个题,总感觉方法太笨,但还没想好别的写法,希望大家帮忙给出经典的方法。题目如下:
打印如下图形,
* * * * * * * * *
* * * *
* * * *
* * * *
* * *
* * * *
* * * *
* * * *
* * * * * * * * *
我的方法如下:
public class Test {
public static void main(String[] args) {
Test.printGraphics();
}

public static void printGraphics() {
int total_row = 17;
int total_col = 9;

for(int col = 1; col <= total_col; col++) {
for(int row = total_row; row >= 1; row--) {
if(col != 1 && col != 9) {
if(row == total_row / 2 + 1 || row == total_row / 2 + col || row == col || row == 2 * col - 1) {
System.out.print("* ");
}else {
System.out.print(" ");
}
}else {
if(col == 1 && row <= total_row / 2 + 1) {
System.out.print("* ");
}else if(col == 1 && row > total_row / 2){
System.out.print(" ");
}

if(col == 9 && row > total_row / 2) {
System.out.print("* ");
}else if(col == 9 && row < total_row / 2){
System.out.print(" ");
}
}
}

System.out.println();
}
}
}
...全文
121 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yufenghua 2010-04-16
  • 打赏
  • 举报
回复
我觉得只用定义三个字符串,用对称来做
aksdn 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sgcl 的回复:]
我认为,解决方法越简单越好。
所以,用下面代码。


Java code

System.out.println(" * * * * * * * * *");
System.out.println("* * * * *");
System.out.println("* * * * *");
System.out.println(" * * * *");
System.out……
[/Quote]
我感觉也是。。。。
sharke118 2010-04-15
  • 打赏
  • 举报
回复
靠 我对你的敬仰犹如滔滔江水
平淡面对 2010-04-15
  • 打赏
  • 举报
回复
我认为,解决方法越简单越好。
所以,用下面代码。


System.out.println(" * * * * * * * * *");
System.out.println("* * * * *");
System.out.println("* * * * *");
System.out.println(" * * * *");
System.out.println(" * * *");
System.out.println(" * * * *");
System.out.println(" * * * *");
System.out.println(" * * * *");
System.out.println("* * * * * * * * *");
sharke118 2010-04-15
  • 打赏
  • 举报
回复
再来个灵活点的
public static void main(String[] args) {
int t = 1;
int[] s = {9*t,4*t,4*t,4*t,3*t,4*t,4*t,4*t,9*t};
for(int i=0;i < s.length;i++){
int n = s[i];
if(i > 0&& i < s.length - 1)
System.out.print("\n ");
else if (i == 0)
System.out.print(" ");
else System.out.print("\n");
for(int k=0;k<n;k++){
System.out.print("*");
}
}
}
sharke118 2010-04-15
  • 打赏
  • 举报
回复
public static void main(String[] args) {
int[] s = {9,4,4,4,3,4,4,4,9};
for(int i=0;i<9;i++){
int n = s[i];
if(i > 0&& i < 8)
System.out.print("\n ");
else if (i == 0)
System.out.print(" ");
else System.out.print("\n");
for(int k=0;k<n;k++){
System.out.print("*");
}
}
}

这个确实很无聊 。。。。。。
Ark032425 2010-04-15
  • 打赏
  • 举报
回复
帮你顶一下
int total_row = 17;
int total_col = 9;
最好用参数传进来
knightzhuwei 2010-04-15
  • 打赏
  • 举报
回复
这到底是个什么东西?????????
zyus1987 2010-04-15
  • 打赏
  • 举报
回复
占坑~!
说实话啊~!感觉你这个题目没啥意义~!

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧