使用#在单个块中打印矩形的代码 [英] Code that will draw a rectangle using # prints in single block

查看:93
本文介绍了使用#在单个块中打印矩形的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授让我:


在java中创建一个读取矩形长度和宽度的代码。使用字符#绘制具有给定尺寸的矩形的绘制。程序应该连续读取数字对。

Create a code in java that will reads the length and the width of rectangle. Drawing of a rectangle with the given dimensions would be drawn using the character "#" . the program should continuously read pairs of numbers.

(第一个长度,然后是宽度)并在输入结束后输出计算出的QPI遇见。

(first length, then width) and outputs the calculated QPI after the end of input is met.


输入:

Input:

输入文件将包含一系列由空格分隔的整数对;每行一对整数。每对中的第一个数字是矩形的长度,而另一个是宽度。

The input file will consist of a series of pairs of integers separated by a space; one pair of integers per line. The first number in each pair is the rectangle's length while the other one is the width.

输出:

每个矩形(带有输入的尺寸)都使用字符'#'输出。

Each rectangle (with the inputted dimensions) is ouputted using the character '#'.

每次绘图后应该有一个空格。

There should be an empty space after each drawing.

样本输入:

1 1
2 2
3 3

样本输出:

#

##
##

###
###
###

这是我创建的代码,但只输入一组数字

This is the code that I create but only 1 set of numbers is only input

import java.io.*;

public class ActivityThree {
    public static void main (String[] args) {
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    String input = "";
    String output = "";
    int a = 0;
    int b = 0;
    int inputParse = 0;
    int outputParse = 0;
    try{
        System.out.print("Enter Length: ");
        input = dataIn.readLine();
        System.out.print("Enter Width: ");
        output = dataIn.readLine();
    }catch( IOException e ){
        System.out.println("Error!");
    }
    inputParse = Integer.parseInt(input);
    outputParse = Integer.parseInt(output);

    for(a = inputParse; a > 0; a--) {
        for(b=0; b < outputParse; b++) {
            if(a >= inputParse)
                System.out.print("#");
            else
                System.out.print("#");
        }
        System.out.print("\n");
        }
    }
}


推荐答案


这是我创建的代码,但只输入一组数字

This is the code that I create but only 1 set of numbers is only input

实际上,您的代码按预期使用打印一个矩形。您只需使用循环结构就可以多次完成此工作。有三个循环结构来完成此任务:

Indeed your code prints a rectangle using # as expected. You just need to use a loop structure to make this work many times. There are three loop structures to accomplish this:


  • while

  • do-while

  • for

  • while
  • do-while
  • for

由您决定使用哪一个(由于这是练习,因此不再显示)。更多信息:

It's up to you to decide which one to use (not showing more since this is an exercise). More info:

  • The while and do-while Statements
  • The for Statement

顺便说一句,你是已经在解决方案中使用 for 循环来控制在一行中编写字符所需的次数。您可以将这些语句中的另一个用于更大的代码块。只需识别您需要重复一段时间的代码块,可能是因为用户输入(提示: System.out.print(输入长度:); )。

By the way, you're already using a for loop in your solution to control how many times you need to write a character in a line. You can use another of these statements for a bigger block of code. Just identify the block of code that you need to repeat an amount of times, probably since the user input (hint: System.out.print("Enter Length: ");).

这篇关于使用#在单个块中打印矩形的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆