虽然Java中的循环打印太快了

时间:2015-12-14 00:03:25

标签: loops while-loop

我是java的新手,并且在我的系绳末端有一点我想要做的事情,如果这是一个非常愚蠢的问题,我很抱歉! 我试图从用户输入打印出绘图区域,然后打印绘图区域中的多个矩形。但是我写的代码是打印出绘图区域和1个矩形,然后要求下一个矩形并打印出来。任何人都可以帮我问多个矩形的大小,然后在最后的绘图区打印出来吗?该程序可以工作,但只是在错误的时间打印,但我无法确定移动到哪里。 我已经在程序的用户端包含了以下代码。任何帮助将不胜感激!

public class DrawingUser
{

    public static void main(String[] args) throws IOException
    {   


        BufferedReader input = new BufferedReader
        (new InputStreamReader(System.in));
        String inputString;

        int rows; int cols; int rowstart; 
        int rowend; int colstart; int colend; char matrix; 


        System.out.print("Input size of row for drawing area ");
        inputString = input.readLine();
        rows = Integer.parseInt(inputString);
        //System.out.println("Row size is " + rows);

        System.out.print("Input size of column for drawing area ");
        inputString = input.readLine();
        cols = Integer.parseInt(inputString);
        //System.out.println("column size is " + cols);

        //while statement to continue printing rectangles 
        //until negative numbers are entered for the rectangle size     
        while (true)
        {
            System.out.print("Input start of rectangle row ");
            inputString = input.readLine();
            rowstart = Integer.parseInt(inputString);

            System.out.print("Input end of rectangle row  ");
            inputString = input.readLine();
            rowend = Integer.parseInt(inputString);

            System.out.print("Input start of rectangle column  ");
            inputString = input.readLine();
            colstart = Integer.parseInt(inputString);

            System.out.print("Input end of rectangle column  ");
            inputString = input.readLine();
            colend = Integer.parseInt(inputString);

            System.out.print("enter a character to create a rectangle ");
            String str = input.readLine();
            char a = str.length() > 0 ? str.charAt(0): '\0';

            //if statement if row/column is negative then stop
            if ((rowstart <0) || (colstart <0))
                    {break;}


        Drawing drawing = new Drawing (rows, cols, 
            rowstart, rowend, colstart, colend);

        drawing.drawArea();

        drawing.fillGrid(a);

        drawing.drawArea();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

如果您希望循环只执行您想要打印矩形的次数,那么您需要在进入循环之前获取输入。

例如......

System.out.print("How many rectangles  ");
inputString = input.readLine();
rectangles= Integer.parseInt(inputString);

然后作为循环中的条件,使用...

while (i < rectangles)...

或使用for循环...

for(int i=0; i < rectangles; i++)...