-1

私たちの教授は問題について修正を加えました:

長方形を描いてください!

ファイル名: ActivityThree.java

入力ファイル: activitythree.in

Maritess は ASCII アートを高く評価し始めています。彼女は思い切って長方形の長さと幅を読み取るプログラムを書き始めました。指定された寸法の長方形の ASCII 描画は、ASCII 文字 '#' を使用して「描画」されます。プログラムは数値のペアを継続的に読み取る必要があります

(最初に長さ、次に幅) を入力し、入力の終わりに達した後に計算された QPI を出力します。

入力:

入力ファイルは、スペースで区切られた一連の整数のペアで構成されます。1 行に 1 組の整数。各ペアの最初の数値は長方形の長さで、もう一方の数値は幅です。

出力:

各長方形 (入力された寸法) は、文字「#」を使用して出力されます。

各描画の後に空白が必要です。

サンプル入力:

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 に答える