-1

我们的教授对这个问题进行了修改:

给我画一个长方形!

文件名:ActivityThree.java

输入文件:activitythree.in

Maritess 开始欣赏 ASCII 艺术。她冒险开始编写一个读取矩形长度和宽度的程序。然后将使用 ASCII 字符“#”“绘制”具有给定尺寸的矩形的 ASCII 图形。程序应该连续读取成对的数字

(首先是长度,然后是宽度)并在满足输入结束后输出计算的 QPI。

输入:

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

输出:

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

每次绘图后都应该有一个空白区域。

样本输入:

1 1

2 2

3 3

5 6

9 10

样本输出:

#
--------


##

##

-------------- 

###

###

###

------------- 

#####

#####

#####

#####

#####

#####

 ----------------------

#########

#########

#########

#########

#########

#########

#########

#########

#########

#########

这仍然是可能的代码:

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

0

虽然我真的不赞成像这样发布完整的代码,但在这种情况下,它似乎对解释如何逐个做你正在寻找的东西很有用。请阅读评论并尝试了解正在发生的事情,而不是简单地复制和粘贴来完成您的作业。当然,如果您有任何具体问题,请随时提出。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ActivityThree {
    public static void main (String[] args) {

        // most of the code here for reading from a file I borrowed from
        // http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

        BufferedReader br = null;
        int length, width;

        try {

            String currentLine;

            // open the file and start reading it
            br = new BufferedReader(new FileReader("activitythree.in"));
            currentLine = br.readLine();

            // only need to do anything if there is something in the file
            if (currentLine != null) {

                // using a do-while loop instead of a while loop so that
                // we can check if there is another line and print out a
                // blank line before the next rectangle
                do {

                    // split the line into two strings, length and width
                    String[] dimensions = currentLine.split(" ");

                    // parse the strings into integers
                    // if they are not proper numbers, set them to 0
                    try {
                        length = Integer.parseInt(dimensions[0]);
                        width = Integer.parseInt(dimensions[1]);
                    } catch (NumberFormatException e) {
                        length = 0;
                        width = 0;
                    }

                    // print out the rectangle
                    printRectangle(length, width);

                    // read the next line and print out a 
                    // blank line if there was something to read
                    currentLine = br.readLine();
                    if (currentLine != null) {
                        System.out.println();
                    }
                } while (currentLine != null);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            // you should always close a file when you're done with it
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void printRectangle(int length, int width) {

        // make sure we're trying to print a rectangle with real dimensions
        if (length <= 0 || width <= 0) {
            return;
        }

        // print `length` lines
        for (int l = 0; l < length; l++) {

            // print `width` #'s in each line
            for (int w = 0; w < width; w++) {

                // using print() because the #'s need to all be on one line
                System.out.print("#");
            }

            // print out a newline when we're done printing  #'s
            System.out.println();
        }
    }
}

这个输入文件:

1 1
2 2
3 3
5 6
9 10

产生这个输出:

#

##
##

###
###
###

######
######
######
######
######

##########
##########
##########
##########
##########
##########
##########
##########
##########
于 2013-07-15T06:57:37.220 回答