有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我将如何使用嵌套for循环来使用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


    }

}

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


共 (2) 个答案

  1. # 1 楼答案

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

    这应该可以奏效,下次一定要让我们知道你已经尝试了什么。:)

  2. # 2 楼答案

    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
    *******
    *******
    *******
    *******
    *******
    *******
    */