在我的boxPrint方法之后重新运行我的主要方法

时间:2016-09-09 18:37:31

标签: java loops methods

您好我正试图通过我的代码重新启动它,但它似乎只是在打印框后退出:

import java.util.Scanner;

public class BoxPrint1 {   

    public static void main(String[] args)    
    {
        Scanner input = new Scanner(System.in);// create scanner object called input  
        // delcare variables

        int column;   
        int row;     

        System.out.printf("%nThis program prints a box");       // prompt user for input
        System.out.printf("%n Please enter # of columns (< 1 to exit):");
        column = input.nextInt();
        System.out.printf("%n Please enter # of rows (< 1 to exit):");
        row = input.nextInt();
        boxPrint(column, row);

        while(column > 0 && row > 0);  
        {    // calls boxPrint method and passes two arguments  
            // prompt user for input
            System.out.printf("%n Please enter # of columns (< 1 to exit):");
            column = input.nextInt();
            System.out.printf("%n Please enter # of rows (< 1 to exit):");
            row = input.nextInt();
        }   
    }// end main


    public static void boxPrint(int column, int row)
    {
        //insert nested for loops below
        for(int i = 0; i < row; i++) 
        {
            for(int j = 0; j < column; j++) 
            {
                    // prints * j times in a row
                System.out.printf("*");
            }
        System.out.println();
        } // end boxPrint method
    }   
    // end class BoxPrint1
}

这是我的代码,通常我得到(它应该输出什么)的输出

等等,但它没有重新循环到我的主要方法并再次请求输入?

2 个答案:

答案 0 :(得分:0)

将主方法更改为如下所示

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);// create scanner object called input
    // delcare variables

    int column;
    int row;

    System.out.printf("%nThis program prints a box");       // prompt user for input
    do {
        System.out.printf("%n Please enter # of columns (< 1 to exit):");
        column = input.nextInt();
        System.out.printf("%n Please enter # of rows (< 1 to exit):");
        row = input.nextInt();
        boxPrint(column, row);
    } while (column > 0 && row > 0);
}

答案 1 :(得分:0)

这可能是因为你在while循环检查后放了一个分号。 它根本不会进入while循环,提示您再次输入输入。