新手问题 Java 打印镂空的菱形,图形

accept · 2016年04月04日 · 最后由 skandhas 回复于 2016年04月14日 · 3676 次阅读
//新手java学习三天,目的:打印镂空菱形。具体图形:
镂空菱形  一共9行分成上5  4 的办法来输出实心的菱形都打印正确但是改代码镂空的时候出现问题上半部分镂空也没问题下半部分镂空打印有误检查不出错误请指教
public class Ex4
{
    public static void main(String[] args)
    {

        for (int i = 1; i <= 5; i++)
        {
            for (int k = 1;k <=5 - i;k++)
            {
                System.out.print(" ");
            }
            for (int j = 1;j <= 2 * i -1;j++)
            {
                if (i == 1)
                {
                    System.out.print("*");
                }
                else
                {
                    if (j == 1 || j == 2 * i -1)
                    {
                        System.out.print("*");
                    }
                    else
                    {
                        System.out.print(" ");
                    }
                }
            }
        System.out.print("\n");
        }
        for (int i = 1;i <= 4;i++)
        {
            for (int k = 1;k <= i;k++)
            {
                System.out.print(" ");
            }
            for (int j = 7;j >= 2 * i - 1;j--)
            {
                if (i == 4)
                {
                    System.out.print("*");
                }
                else
                {
                    if (j == 1 || j == 9 - 2 * i)
                    {
                        System.out.print("*");
                    }
                    else
                    {
                        System.out.print(" ");
                    }
                }
            }
        System.out.print("\n");
        }
    }
}

这个好像讨论过,最佳写法:

puts <<EOF
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
EOF

#1 楼 @rei :plus1:

上面一坨坨代码有点不想看
还是直接打印直接

3 楼 已删除

ruby 实现太简单了!谢谢两位

这个简单实现,跟 ruby 没有关系,你直接在 Java 里面把图形打出来,也是一样的。 😂

public class Ex4 {
  public static void main(String[] args) {
    int size = 5;

    for ( int y = 0; y < size; ++y ) {
      for ( int x = 0; x < 2 * size; ++x ) {
        if ( (size - 1 - x) == y || (size - 1 + y) == x ) {
          System.out.print("*");
        } else {
          System.out.print(" ");
        }
      }
      System.out.println();
    }

    for ( int y = 0; y < size - 1; ++y ) {
      for ( int x = 0; x < 2 * size; ++x ) {
        if ( x == y + 1 || 2 * size - 3 == x + y ) {
          System.out.print("*");
        } else {
          System.out.print(" ");
        }
      }
      System.out.println();
    }
  }
}

复习了一下 java。x,y 是坐标系

送个 ruby 版本

def print_lingxing line_num
  raise "朕不伺候偶数" if line_num.even?
  line_num.times do |i|
    if i < line_num / 2
      puts "*".rjust(line_num / 2 - i + 1) + "*".rjust(i * 2 + 1)[1..-1]
    else
      puts "*".rjust(i + 1 - line_num / 2) + "*".rjust((line_num - i) * 2 - 1)[1..-1]
    end
  end
end

print_lingxing 9

#7 楼 @piecehealth 漂亮。诶,和 java 的写法比 ruby 的跟开挂一样

#6 楼 @z_ach 谢谢你,最近回家了一趟,一直没上社区!

#7 楼 @piecehealth 谢谢!对比学习更容易理解。

@accept 另一种写法

def draw_diamond(count)
  raise "Orz... " unless count.odd?
  lines = (1...count).select(&:odd?)
                     .inject(['*']) { |m, i| m << "*#{' ' * i}*"}
  [*lines, *lines.reverse.tap(&:shift) ].each { |l| puts l.center(count) }
end

draw_diamond(9)
draw_diamond(15)
需要 登录 后方可回复, 如果你还没有账号请 注册新账号