0

嗨,我正在尝试创建一个 checkArg 方法,该方法检查用户是否在应该输入整数参数时输入了字符串,如果用户输入了 3 个以上的参数,那么如果这些条件不是,则告诉他们“请输入正确的值”见面,然后再次询问并继续程序。我的代码底部有一个粗略的大纲。

我尝试在底部的 main 中调用我的 checkArg 方法,以比较参数“大小”。它可以编译,但不会告诉我“请输入正确的值”然后继续程序。

我的代码:

import java.util.*;

public class trominoZ2 {
    //create a drawing panel of width=400px and height=400px

    private int[][] grid;
    private int currentNum;

    // Pre-condition: size must be a perfect power of 2 and 0<=x<size, 0<=y<size
    // Post-condition: creates an empty tromino object with dimensions size x size.
    public void tromino(int size, int x, int y) {

        int actualsize = 1;
        while (actualsize < size) actualsize*=2;     //actualsize = actualsize * 2

        // Make sure the grid size is a perfect power of 2.
        grid = new int[actualsize][actualsize];
        currentNum = 1;

        // Fill in the grid with all empty squares.
        for (int i=0; i<actualsize; i++) {
            for (int j=0; j<actualsize; j++) {
                grid[i][j] = 0;
            }
        }

        // This represents the original hole in the tromino.
        grid[x][y] = -1;
    }

    // Wrapper call for recursive method.
    public void tile() {
        tileRec(grid.length, 0, 0);
    }

    private void tileRec(int size, int topx, int topy) {

        // No recursive case needed here, just fill in your one tromino...
        if (size == 2) {

            // Fill in the one necessary tromino. The hole is identified by a
            // non-zero number, so don't fill in that one square.   
            for (int i=0; i<size; i++) 
                for (int j=0; j<size; j++)
                    if (grid[topx+i][topy+j] == 0)
                        grid[topx+i][topy+j] = currentNum;

            // Advance to the next tromino.
            currentNum++;
        }

        // Recursive case...
        else {

            // Find coordinates of missing hole
            int savex=topx, savey=topy;

            for (int x=topx; x<topx+size; x++) 
                for (int y=topy; y<topy+size; y++)
                    if (grid[x][y] != 0) {
                        savex = x;
                        savey = y;
                    }

            // Hole in upper left quadrant.     
            if (savex < topx + size/2 && savey < topy + size/2) {

                // Recursively tile upper left quadrant.
                tileRec(size/2, topx, topy);

                // Fill in middle tromino
                grid[topx+size/2][topy+size/2-1] = currentNum;
                grid[topx+size/2][topy+size/2] = currentNum;
                grid[topx+size/2-1][topy+size/2] = currentNum;

                // Advance to the next tromino
                currentNum++;

                // Now we can make our three other recursive calls.
                tileRec(size/2, topx, topy+size/2);
                tileRec(size/2, topx+size/2, topy);
                tileRec(size/2, topx+size/2, topy+size/2);

            }

            // Hole in upper right quadrant
            else if (savex < topx + size/2 && savey >= topy + size/2) {

                // Recursively tile upper right quadrant.
                tileRec(size/2, topx, topy+size/2);

                // Fill in middle tromino
                grid[topx+size/2][topy+size/2-1] = currentNum;
                grid[topx+size/2][topy+size/2] = currentNum;
                grid[topx+size/2-1][topy+size/2-1] = currentNum;

                // Advance to the next tromino
                currentNum++;

                // Now we can make our three other recursive calls.
                tileRec(size/2, topx, topy);
                tileRec(size/2, topx+size/2, topy);
                tileRec(size/2, topx+size/2, topy+size/2);

            }

            // Hole in bottom left quadrant
            else if (savex >= topx + size/2 && savey < topy + size/2) {

                // Recursively tile bottom left quadrant.
                tileRec(size/2, topx+size/2, topy);

                // Fill in middle tromino
                grid[topx+size/2-1][topy+size/2] = currentNum;
                grid[topx+size/2][topy+size/2] = currentNum;
                grid[topx+size/2-1][topy+size/2-1] = currentNum;

                // Advance to the next tromino
                currentNum++;

                // Now we can make our three other recursive calls.
                tileRec(size/2, topx, topy);
                tileRec(size/2, topx, topy+size/2);
                tileRec(size/2, topx+size/2, topy+size/2);
            }
            else {

                // Recursively tile bottom right quadrant.
                tileRec(size/2, topx+size/2, topy+size/2);

                // Fill in middle tromino
                grid[topx+size/2-1][topy+size/2] = currentNum;
                grid[topx+size/2][topy+size/2-1] = currentNum;
                grid[topx+size/2-1][topy+size/2-1] = currentNum;

                // Advance to the next tromino
                currentNum++;

                // Now we can make our three other recursive calls.
                tileRec(size/2, topx+size/2, topy);
                tileRec(size/2, topx, topy+size/2);
                tileRec(size/2, topx, topy);
            }

        } // end large if-else

    } // end tileRec


    // Prints out the current object.
    public void print() {

        for (int i=0; i<grid.length; i++) {
            for (int j=0; j<grid[i].length; j++)
                System.out.print(grid[i][j] + "\t");
            System.out.println();
        }
    }

    public static int checkArg(int ch){
        String str = "";
        char[] all = str.toCharArray();
        for(int i = 0; i < all.length;i++) {
            if(!Character.isDigit(all[i])) {
                System.out.println("Please retype a correct value");            
            }
        }
        return ch;
    }

    public static void main(String[] args) {

        Scanner stdin = new Scanner(System.in);

        // Get user input...
        int size = stdin.nextInt();            
        int x = stdin.nextInt();
        int y = stdin.nextInt();

        if(checkArg(size) != size){
            checkArg(size);
        } else if (x != x){
            System.out.println("Please retype a correct value");
        } else if (y != y){
            System.out.println("Please retype a correct value");
        }

        // Create our object and tile it!
        tromino thisguy = new tromino(size, x, y);

        //tile grid with trominos
        thisguy.tile();

        // Print romino grid.
        thisguy.print();

    }
}

如何让我的 checkArg 方法检查用户输入的参数是否超过 3 个,或者它是否不是整数参数?

4

1 回答 1

0

你可以做这样的事情。让该方法返回一个布尔值。然后检查 args 中的main响应boolean。并一直循环直到方法返回true

public static boolean checkArg(String line){
    String[] args = line.split("\\s+");

    if (args.length != 3) {
        return false;
    }

    try {
        int num1 = Integer.parseInt(args[0].trim());
        int num1 = Integer.parseInt(arss[1].trim());
        int num3 = Integer.parseInt(args[2].trim());
    } catch (Exception ex) {
        System.out.println("Must enter 3 integers");
        return false;
    }

    return true;
}

用于main

public static void main(String[] args) {

    Scanner stdin = new Scanner(System.in);
    String input = stdin.nextLine();

    while (!checkArgs(input)) {
        input = stdin.nextLine();
    }

    String[] args = input.split("\\s+");
    int size = Integer.parseInt(args[0].trim());
    int x = Integer.parseInt(arss[1].trim());
    int y = Integer.parseInt(args[2].trim());


    // Create our object and tile it!
    tromino thisguy = new tromino(size, x, y);

    //tile grid with trominos
    thisguy.tile();

    // Print romino grid.
    thisguy.print();
}
于 2013-11-12T07:33:00.097 回答