Ejemplo n.º 1
0
  /**
   * a method that get the input from user and print out the magic square this is a recursive method
   */
  public void getInput() {
    System.out.print("please enter a positive odd integer __ or 0 to stop");
    Scanner scan = new Scanner(System.in);

    // check if the input is an intger
    if (!scan.hasNextInt()) {
      System.err.println("please enter a positive odd integer");
      getInput();

    } else {
      int input = Integer.parseInt(scan.next());
      if (input != 0) {
        if (input < 0 || input % 2 == 0) {
          System.err.println("please enter a positive odd integer");
          getInput();
        } else {
          OddMagicSquare s = new OddMagicSquare(input);
          String str = s.toString();
          System.out.println("a magic square with dimension of " + input + "X" + input);
          System.out.println(str);
          getInput();
        }
      }
    }
    return;
  }
Ejemplo n.º 2
0
 /** @param args */
 public static void main(String[] args) {
   OddMagicSquare s = new OddMagicSquare(7);
   String str = s.toString();
   System.out.println("a test with input of 7");
   System.out.println(str);
   boolean tf = isMagic(s.getSquare());
   System.out.println("is the square magic: " + tf);
   s.getInput();
 }