在 Java 中打印星号矩形

Printing a rectangle of asterisks in Java

以下是打印完全由星号 (*) 组成的矩形的源代码,其中还包含测试 class。

import javax.swing.JOptionPane ;

/**
 * A class to print block rectangles.
 */
class RectanglePrinter
{
    // instance var's
    int height ;            // height of rectangle (i.e. number of segments)    
    int width ;             // width of each segment(i.e. number of "*"s printed) 

    /**
     * Create a RectanglePrinter object.
     * @param height height of rectangle (i.e., number of lines to print)
     * @param width width of rectangle (i.e., number of '*'s per line
     */
    public RectanglePrinter(int height, int width)  // constructor
    {
        this.height = height ;
        this.width = width ;
    }

    /**
     * Prints one line of a rectangle, by printing exactly "width" asterisks
     */ 
    public void printSegment()
    {
        // write the body of this method here


    }

    /**
     * Prints a rectangle exactly "height" lines in height.  Each line is 
     * printed via a call to method printSegment
     */ 
    public void printRectangle()
    {
        System.out.println("Printing a " + height + " x " + width + " rectangle:") ;
        // write the body of this method here


    }

}  // end of class rectanglePrinter definition

public class RectanglePrinterTest
{
    public static void main (String [] args)
    {
        String input = JOptionPane.showInputDialog
                                   ("What is the height of the rectangle?") ;
        int height = Integer.parseInt(input) ;

        input = JOptionPane.showInputDialog
                            ("What is the width of the rectangle?") ;

        int width = Integer.parseInt(input) ;

        RectanglePrinter r = new RectanglePrinter(height, width) ;

        System.out.println() ;
        r.printRectangle() ;
        System.out.println() ;
    }
}

在它说要填写方法体的部分,我被指示使用for循环打印出星号。我想我对如何执行 printSegment() 方法有一个基本的想法:

for (int w = 1; w <= width; w++)
            {
                System.out.println("*");
            }

但是从那里开始,我不确定在 printRectangle 方法中要做什么。从代码中的注释来看,我认为我应该在调用 printSegment 方法的 printRectangle 方法中编写一个 for 循环,但我认为您不能在另一个方法的主体中调用 void 方法。对此的任何帮助将不胜感激。

更新:

我试图在 printRectangle() 的主体中使用以下代码

for (int h = 1; h <= height; h++)
                {
                    printSegment();
                }

在 运行 代码并输入高度和宽度后,我收到以下输出:

Printing a 6 x 7 rectangle:
*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

现在我至少可以打印出星号了,所以我只需要知道如何修改代码,使输出是一个长方形的星号块。

更新 #2。

我想出了解决办法。这是让我得到我正在寻找的结果的代码。

import javax.swing.JOptionPane ;

/**
 * A class to print block rectangles.
 */
class RectanglePrinter
{
    // instance var's
    int height ;            // height of rectangle (i.e. number of segments)    
    int width ;             // width of each segment(i.e. number of "*"s printed) 

    /**
     * Create a RectanglePrinter object.
     * @param height height of rectangle (i.e., number of lines to print)
     * @param width width of rectangle (i.e., number of '*'s per line
     */
    public RectanglePrinter(int height, int width)  // constructor
    {
        this.height = height ;
        this.width = width ;
    }

    /**
     * Prints one line of a rectangle, by printing exactly "width" asterisks
     */ 
    public void printSegment()
    {
        // write the body of this method here
                for (int w = 1; w <= width; w++)
                {
                    for (int h = 1; h <= height; h++)
                    {
                        System.out.print("*");
                    }
                    System.out.println();
                }


    }

    /**
     * Prints a rectangle exactly "height" lines in height.  Each line is 
     * printed via a call to method printSegment
     */ 
    public void printRectangle()
    {
        System.out.println("Printing a " + height + " x " + width + " rectangle:") ;
        // write the body of this method here
                printSegment();


    }

}  // end of class rectanglePrinter definition

public class RectanglePrinterTest
{
    public static void main (String [] args)
    {
        String input = JOptionPane.showInputDialog
                                   ("What is the height of the rectangle?") ;
        int height = Integer.parseInt(input) ;

        input = JOptionPane.showInputDialog
                            ("What is the width of the rectangle?") ;

        int width = Integer.parseInt(input) ;

        RectanglePrinter r = new RectanglePrinter(height, width) ;

        System.out.println() ;
        r.printRectangle() ;
        System.out.println() ;
    }
}

不同之处:我在 printSegment() 方法的主体中添加了一个嵌套的 for 循环,而不是使用 System.out.println("*"); ,在最内层循环中使用 System.out.print("*") ,在外层循环中使用 System.out.println() ,这导致输入 5 x 7 的输出如下:

Printing a 5 x 7 rectangle:
*****
*****
*****
*****
*****
*****
*****

这听起来确实像是您应该在 printRectangle 中调用 printSegment 进行高度次数的循环。

要在 printRectangle 中调用您的 printSegment 方法,只需编写 printSegment(); 即可调用它。

因为它在同一个 class 中,所以您只需调用它即可。如果它是从另一个 class 调用的,您可以检查 RectanglePrinterTest main 方法如何使用 r.printRectangle(); 调用 printRectangle,其中 rRectanglePrinter 的一个实例class.

因为它是一个无效的方法,所以它不会 return 任何东西,所以你不需要做任何其他事情。 :)

总而言之,您的方向是正确的!继续前进,尝试你认为正确的方法,你一定会弄明白的。

您需要使用“\n”字符串来打印新行。每次宽度或行命令完成时,它应该打印“\n”。

使用您的代码片段,我现在可以成功地水平打印一个矩形。

赫尔辛基 CS MOOC (http://mooc.cs.helsinki.fi/) 在其学习 Java 课程中介绍了此任务。

public class MoreStars {

    public static void main(String[] args) {    
        printRectangle(10, 6);
    }

    public static void printRectangle(int width, int height) {
        int i;
        int j;
        for ( i = 0; i < height; i++ ) {
            for ( j = 0; j < width; j++) {
                printStars(width);
            }
            System.out.println("");
        }                
    }

    public static void printStars(int amount) {   
        System.out.print(" * ");
    }
}