我如何使用嵌套for循环使用java从星号中制作实心矩形?

时间:2018-03-14 04:21:54

标签: java

import java.util.Scanner;

public class Box {

    public static void main(String[] args) {


        Scanner keyboard = new Scanner(System.in);

        int length;
        int width;
        int rectangle;

        // input and output here

        System.out.print("Input the width of the box: ");
        width = keyboard.nextInt();

        System.out.print("Input the length of the box: ");
        length = keyboard.nextInt();

        // use nested for loops here


    }

}

这就是我程序的基础,我需要一个程序,使用嵌套的for循环从星号中打印出一个实心的矩形。

2 个答案:

答案 0 :(得分:0)

for(int i = 0; i< height; i++) {
    for(int j=0; j<width; j++) {
        System.out.print("*");
    }
    System.out.println();
}

这应该可以解决问题,下次确保让我们知道您已尝试过的内容。 :)

答案 1 :(得分:0)

import java.util.Scanner;

public class q1 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        int length;
        int width;
        int rectangle;

        // input and output here

        System.out.print("Input the width of the box: ");
        width = keyboard.nextInt();

        System.out.print("Input the length of the box: ");
        length = keyboard.nextInt();
        for(int i = 0; i< length; i++) {
            for(int j=0; j<width; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

/*
 * program output

Input the width of the box: 6
Input the length of the box: 7
*******
*******
*******
*******
*******
*******
*/