-3

我将如何使用三种方法来产生这样的输出?

Please enter the fill character: "z"
Please enter the size of the box (0 to 80): "3"
+---+
|zzz|
|zzz|
|zzz|
+---+

我的代码能够生成一个框,但是我在理解使用其他方法在它周围创建边框时遇到了问题。

 import java.util.Scanner;
public class SolidBoxes
{
    public static void main(String[] args)
    {
        int start = 0;
        Scanner scan = new Scanner(System.in);

        System.out.print("Please enter the fill character: ");
        String character = scan.next();

        System.out.print("Please enter the size of the box (0 to 80): ");
        int size = scan.nextInt();

        if ( size > 80 || size < 0)
        {
            System.out.println("Please enter the size of the box (0 to 80): ");
            size = scan.nextInt();
        }

        for ( int i = 0; i < size; i++)
        {

            System.out.println();

            for ( int j = 0; j < size; j++)
            {   
            System.out.print(character);
            }

        }   
    }
}

这给了我输出:

Please enter the fill character: z
Please enter the size of the box (0 to 80): 3

zzz
zzz
zzz

如何为“+---+”和另一种方法“|”添加另外两种方法?

4

1 回答 1

0

方法提供了一种将部分代码划分为您想要执行的各种任务的方法。通常,方法将执行特定任务所需的所有操作。当调用代码想要执行您在方法中实现的特定任务时,从其他代码调用方法。例如,如果您想在 main 中绘制一个形状,您可能有一个看起来像这样的方法:

    public void drawShape(...){
       ...
       //Put specific code to draw the shape here
       ...
    }

然后在你的 main 中,只要你想绘制那个形状,你就可以像这样调用方法:

     drawShape(...);

在上面的方法public中,访问修饰符告诉我们这个方法对任何可以“看到”它的代码都是公开的。该void部分是返回类型,在这种情况下它不返回任何内容。是drawShape方法名称。

在您的情况下,您似乎需要提供三种单独的方法。首先,您应该定义一种方法来输出第一行,然后获取填充字符并将其返回给主行。然后提供第二种方法来输出第二行并将框的大小返回给main。最后提供第三种方法来根据您收到的前两个输入输出框。当你写完这三个方法后,然后从 main 中以正确的顺序调用它们来运行完整的程序。

于 2012-10-23T03:29:03.773 回答