用java打印菱形。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java打印菱形。相关的知识,希望对你有一定的参考价值。

参考技术A

一、整体实现思路:菱形9行9列可以将菱形分成上下两个三角形,分析每行空格数和星号个数的关系。

二、一个三角形的实现思路:以输出一个空心三角形为例:

(1)空格的输出按-1递减,字符的输出按等差数列,公差为2

(2)判断第一行和最后一行照常输出。

(3)中间行仅输出两个字符。

三、菱形分实心和空心两种,其代码及解析分别如下:

(一)实心菱形。

1、函数代码:

2、执行效果:

(二)空心菱形:

1、函数代码如下:

2、执行效果:

扩展资料:

java打印的输出方式解析:

1、print():print就是一般的标准输出,但是不换行。

2、println():println和print基本没什么差别,就是最后会换行。

要分辨清楚System.out.print()和System.out.println()的区别。System.out.print()是单纯的输出,而System.out.println()的功能是输出内容后换行,到达下一行,在使用时要注意区分两者之间的不同。

用“for”循环在java中打印出菱形图案

【中文标题】用“for”循环在java中打印出菱形图案【英文标题】:Printing out a rhombic pattern in java with "for" loop 【发布时间】:2015-11-05 13:29:03 【问题描述】:

我很难用 Java 进行学校练习。我们被要求打印出这个模式:

+++++++++++++++++++++++++++++++++++++++++++++
 +++++++  +++++++  +++++++  +++++++  +++++++ 
  +++++    +++++    +++++    +++++    +++++  
   +++      +++      +++      +++      +++   
    +        +        +        +        +    
   +++      +++      +++      +++      +++   
  +++++    +++++    +++++    +++++    +++++  
 +++++++  +++++++  +++++++  +++++++  +++++++ 
+++++++++++++++++++++++++++++++++++++++++++++
 +++++++  +++++++  +++++++  +++++++  +++++++ 
  +++++    +++++    +++++    +++++    +++++  
   +++      +++      +++      +++      +++   
    +        +        +        +        +    
   +++      +++      +++      +++      +++   
  +++++    +++++    +++++    +++++    +++++  
 +++++++  +++++++  +++++++  +++++++  +++++++ 
+++++++++++++++++++++++++++++++++++++++++++++
 +++++++  +++++++  +++++++  +++++++  +++++++ 
  +++++    +++++    +++++    +++++    +++++  
   +++      +++      +++      +++      +++   
    +        +        +        +        +    
   +++      +++      +++      +++      +++   
  +++++    +++++    +++++    +++++    +++++  
 +++++++  +++++++  +++++++  +++++++  +++++++ 
+++++++++++++++++++++++++++++++++++++++++++++

我可以做一个三角形或沙漏形状,但我不能让它水平重复。

这是我目前所拥有的:

int a = 9;
char b = '+';
char c = ' ';

int i_buffer = a;
int i_leer = 1;
for (int i = 0; i < a; i++) 
    for (int z = i_buffer; z > 0; z--)
        System.out.print(b);

    System.out.println();

    i_buffer = i_buffer - 2;
    if (i_buffer < 0)
        break;

    for (int z = i_leer++; z > 0; z--)
        System.out.print(c);

【问题讨论】:

循环重复println()之前的操作5次? 菱形是新金字塔。 大提示:由于这具有明显的水平和垂直周期性,请使用模运算符(%)。本质上,整个输出是一个具有一定数量的行和列的矩阵,每个元素是一个加号或一个空格。因此,归结为编写一个函数 f(row, col),例如返回 1 表示 + 或 0 表示空格。 @tobias_k 我试过了,但它没有打印任何空格......它所做的只是很多加号。 @MarounMaroun 哈哈大笑 【参考方案1】:

这里有一个解决方案,可以根据需要在 x 和 y 维度上以每个尺寸创建此模式。实际发生的事情写成评论。

public static void main(String[] args) 
    test();


public static void test() 
    // Print it three times under each other, 5 times next to each other 
    // With a size of 9 plus
    printHourglass(3,5,9);


private static void printHourglass(int column, int times, int size) 
    for(int i = 0;i<column;++i) 
        // If it is the first iteration print the first line, otherwhise
        // Leave out the first only plus line
        printHourglassRow(times, size, i == 0 ? 0 : 1);
    


private static void printHourglassRow(int times, int size, int start) 
     // If it is not the first hourglass don´t print the first only plus row
    int printableStars = start == 0 ? size : size-2;
    int printableSpaces = start == 0 ? 0 : 1;
    // If the size is even we need to print one row less
    size = size % 2 == 0 ? size-1:size;
    for(int i = start;i<size;++i) 
        // Decides if we should print whitespaces or spaces first.
        // only The first and last row start with the plus 
        int mode = (i == size || i == 0) ? 1 : 0;
        //System.out.println(mode);
        //System.out.println(printableStars);
        // We are printing each column, we start with column 0
        int column = 0;
        // We print until we did reach the last column.
        while(column<times*size) 
            // If we are in print plus mode the next thing we print
            // will be a space, so switch mode and print stars as long as we should
            if(mode == 1) 
                mode = 0;
                for(int j = 0;j<printableStars;++j) 
                    printPlus();
                    ++column;
                
            // If we are in print whitespace mode the next thing we print
            // will be a space, so switch mode and print whitespaces as long as we should
             else 
                mode = 1;
                // If we reached the second hourglass (column > size/2)
                // Then we need to print twice as much whitespaces
                int sizeHalf = size % 2 == 0? size/2 : size/2+1; 
                for(int j = 0;column > sizeHalf ? j<printableSpaces*2:j<printableSpaces;++j) 
                    printSpace();
                    ++column;
                
            
        
        // We are not in the middle of the hourglass so
        // We print two stars less and one whitespace more.
        if(size - 2*i > 2) 
            ++printableSpaces;
            printableStars -= 2;
        // If we are reaching the point where we are in the middle
        // Of the houreglass we need to print one whitespace less 
        // and two plus more.
         else 
            --printableSpaces;
            printableStars += 2;
        
        // Linebreak
        System.out.println();
    


private static void printPlus() 
    System.out.print("+");


private static void printSpace() 
    System.out.print(" ");

【讨论】:

非常感谢!尽管我们使用您给我的所有代码还为时过早,但我可以根据需要对其进行调整。你帮了大忙! :)

以上是关于用java打印菱形。的主要内容,如果未能解决你的问题,请参考以下文章

JAVA的for循环打印菱形,谁能给讲一讲。

用java程序打印菱形

用“for”循环在java中打印出菱形图案

java打印一个菱形

用Java实现菱形的打印输出

用java编写菱形