-3

My professor ask me to:

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.

(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.

Output:

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

There should be an empty space after each drawing.

Sample Input:

1 1
2 2
3 3

Sample Output:

#

##
##

###
###
###

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");
        }
    }
}
4

1 回答 1

1

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

#实际上,您的代码按预期打印了一个矩形。您只需要使用循环结构即可多次完成这项工作。有三种循环结构可以完成此操作:

  • while
  • do-while
  • for

由您决定使用哪一个(由于这是一个练习,所以没有显示更多)。更多信息:


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

于 2013-07-15T03:18:23.467 回答