2012-02-12 75 views
0

我在做一個magicsquare程序,允許用戶輸入數字> 0來形成magicsquare。多麼神奇的正方形,幾乎是一個正方形,這意味着n必須有n(平方)的數字。就像ticTacToe一樣,所有的行,列和對角線都有相同的總和,被認爲是一個神奇的正方形當我運行我的程序時,它總是會混淆2D數組集合,並聲稱這組數字通常是magicsquare,並不一定如此。請幫忙!程序未按預期運行

import java.util.Scanner; 

public class SquareRunner 
{ 
public static void main(String[] args) 
{ 
    Scanner in = new Scanner(System.in); 
    Square test = new Square(); 
    System.out.println("Enter a row of integers. When you are finished, type 'n' in a new line"); 
    boolean flag = false; 
    while(!flag) 
    { 
     String numbers = in.next(); 
     if(numbers.equals("n")) 
     flag = true; 
     else 
     test.add(numbers); 
    } 
    test.isMagic(); 

} 
} 

public class Square 
{ 
    private int[][] values; 
    private int row; 

public Square() 
{ 
    row = 0; 
} 

public void add(String numbers) 
{ 
    int b = 1; 
    int amount = numbers.length(); 
    values = new int[amount][amount]; 

    for(int j =0;j<amount;j++) 
    { 
     String a = numbers.substring(j,b); 
     int convert = Integer.parseInt(a); 
     values[row][j] = convert; 

     b++; 
    } 
    row++; 

} 


public Boolean isMagic() 
{ 
    int checkAmountColumns = values[0].length; 
    int checkAmountRows = values.length; 
    int isSquare = checkAmountColumns * checkAmountRows; 

     for(int q = 0;q<values.length;q++) 
     { 
      for(int w=0;w<values[0].length;w++) 
      { 
       int checkZero = values[q][w]; 
       if(checkZero == 0) 
       { 
        System.out.print("To be a perfect square, your number of rows and columns, n must be a perfect "); 
        System.out.println("Square i.e. 9 total numbers is 3 numbers per row"); 
        return false; 
       } 
      } 
     } 
    if(checkAmountColumns != checkAmountRows || Math.sqrt(isSquare) != checkAmountColumns) 
    { 
     System.out.print("To be a perfect square, your number of rows and columns, n must be a perfect "); 
     System.out.println("Square i.e. 9 total numbers is 3 numbers per row"); 
     return false; 
    } 

    else 
    { 
     int magicNumber = 0; 
     int counter = 0; 
     int compareTo = 0; 

     //row to row 
     for(int i =0;i<values.length;i++) 
     { 
      for(int j = 0;j<values[0].length;j++) 
      { 

       values[i][j] += compareTo; 
       if(counter == 0) 
       values[i][j] += magicNumber; 

      } 
      counter ++; 
      compareTo = 0; 
      if(compareTo != magicNumber) 
      { 
       System.out.println("This Selection of numbers is not a perfect square"); 
       return false; 
      } 
     } 

     //column to column 
     for(int i =0;i<values[0].length;i++) 
     { 
      for(int j = 0;j<values.length;j++) 
      { 

        values[j][i] += compareTo; 
        if(counter == 0) 
        values[j][i] += magicNumber; 

       } 
       counter ++; 
       compareTo = 0; 
       if(compareTo != magicNumber) 
       { 
       System.out.println("This Selection of numbers is not a perfect square"); 
       return false; 
       } 

     } 
     System.out.println("This selection of numbers is a MagicSquare!"); 
     return true; 

    } 
} 
} 
+3

這功課嗎? – 2012-02-12 18:36:06

回答

0

我注意到的第一件事是您的add()方法可能無法正常工作。每次調用它時,都會用新分配的數組覆蓋以前的values成員。這會丟棄您輸入的上一行。

+0

我有一種感覺,它確實..林不知道如何解決這個問題。你有什麼主意嗎? – James 2012-02-12 18:50:14

+0

其中一個想法是在構造函數中分配'values'數組*一次*(傳遞'size'參數),然後在'add()'方法中填充值。 – 2012-02-12 19:20:11

+0

感謝你們兩位 – James 2012-02-12 19:24:59

0

與Greg相同的答案。 將此添加到您的添加方法並刪除您的值數組的初始化。

if(values == null){values = new int[amount][amount];}