簡體   English   中英

在 Java 中打印星號矩形

[英]Printing a rectangle of asterisks in Java

以下是打印完全由星號 (*) 組成的矩形的源代碼,還包括一個測試類。

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獲取高度時間。

打電話給你的printSegment內部方法printRectangle簡單地寫printSegment(); 調用它。

由於它在同一個類中,這就是您調用它所需要做的全部工作。 如果它是從另一個類調用的,您可以檢查RectanglePrinterTest主方法如何使用r.printRectangle();調用printRectangle r.printRectangle(); 其中rRectanglePrinter類的實例。

由於它是一個 void 方法,它不會返回任何內容,因此您無需執行任何其他操作。 :)

所以總而言之,你走在正確的軌道上! 繼續前進並嘗試您認為正確的方法,您一定會弄明白的。

您需要使用“\\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(" * ");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM