有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我怎样才能从计算机中读取数字。txt文件并将其放入2D数组中?

这是一个学校项目。我需要读一本书上的数字。txt文件,并将它们放入数组中。当它们在一个数组中之后,我需要将其传递到另一个类中进行数学运算并比较这些数字。唯一的问题是,我无法让代码读取。txt文件或使其成为数组

我需要使用

    if (sq.isMagicSquare())

要将数组传递给类Square,但会产生错误:

required: int[][]

found: no arguments

reason: actual and formal lists differ in length

public class MagicSquareTester
{
public static void main() throws IOException
{
    Square sq = null;       

    System.out.println("Enter the name of your data file (magicData.txt):");
    Scanner keyboard = new Scanner(System.in);
    String fileName = keyboard.nextLine();              // input data file name from keyboard

    Scanner inFile = new Scanner(new File (fileName));
    int sqSize = inFile.nextInt();                      // read the size 

    while (sqSize != -1)
    {
        sq = new Square(sqSize, inFile);
        if (sq.isMagicSquare())  //will return true or false
            System.out.println("\tWe have a Magic Square!");
        else
            System.out.println("\tThis is NOT a Magic Square.");

        System.out.println(sq);
        System.out.println();
        sqSize = inFile.nextInt();
    }

    System.out.println("Of the " + sq.getTotalTested() + " squares tested " + sq.getMagicCount() + " were magic square(s)" );
}
}

public class Square
{
    Scanner scan = new Scanner(System.in); //has been imported correctly, btw
    int tested = 0, areMagic = 0, sqSize;
    boolean magic;
    int[][] Square;

    public Square(int sqSize, Scanner inFile)
    {
        Square = new int [sqSize] [sqSize];
    }

    public void readSquare(Scanner inFile)
    {
        for(int row = 0; row < sqSize; row++)
            for(int col = 0; col < sqSize; col++)
            {
                Square[row][col] = inFile.nextInt();
                tested++;
            }
    }

    public boolean isMagicSquare(int[][] array)
    {
        Sums testMagic = new Sums();
        int rows = testMagic.sumRows(array);
        int cols = testMagic.sumCol(array);
        int diagonals = testMagic.sumDiagonal(array);

        if((rows == cols) && (cols == diagonals) && (diagonals == rows))
        {
            magic = true;
            areMagic++;
        }
        else 
            magic = false;
        return magic;
    }

    public int getMagicCount()
    {
        return areMagic;
    }

    public int getTotalTested()
    {
        return tested;     
    }
}



public class Sums
{
    int sum = 0, lastSum = 0, counter1, counter2, counter3;
    boolean magic = true;

    public int sumRows(int [][] array)
    {
        for (int row = 0; row < array.length; row++) 
        {
            sum = 0;
            for (int col = 0; col < array.length; col++) 
            {
                sum += array[row][col];
                System.out.print(sum + " ");
                if (lastSum == sum) 
                {
                    lastSum = sum;
                    counter1++;
                }
                else if (lastSum != sum) 
                    {
                        magic = false;
                        System.out.println("This is not a magic square");
                        row = array.length;
                        col = array.length;
                    }
            }
        }
        return counter1;
    }

    public int sumCol(int [][] array)
    {
        for (int col = 0; col < array.length; col++) 
        {
            sum = 0;
            for (int row = 0; row < array.length; row++) 
            {
                sum += array[row][col];
                System.out.print(sum + " ");
                if (lastSum == sum) 
                {
                    lastSum = sum;
                    counter2++;
                }
                else if (lastSum != sum) 
                    {
                        magic = false;
                        System.out.println("This is not a magic square");
                        row = array.length;
                        col = array.length;
                    }
            }  
        }
        return counter2;
    }

    public int sumDiagonal(int [][] array)
    {
        int diagonal1 = 0, diagonal2 = 0;
        for (int col = 0; col < array.length; col++) 
        {
            sum = 0;
            for (int row = 0; row < array.length; row++) 
            {
                if(row == col)
                {
                    sum += array[row][col];
                    System.out.print(sum + " ");
                    diagonal1 = sum;
                }
            }
        }
        for (int col = 0; col < array.length; col--) 
        {
            sum = 0;
            for (int row = 0; row < array.length; row++) 
            {
                if((row + col) == array.length - 1)
                {
                    sum += array[row][col];
                    System.out.print(sum + " ");
                    diagonal2 = sum;
                }
            }
        }

        if(diagonal1 == diagonal2)
        {
            magic = true;
            counter2 = counter3;
        }
        else
            counter3 = 0;

        return counter3;
    }
}

另外,如果我的代码看起来格式怪异,我也会道歉。我以前从未在这里发帖,我正在尽我最大的努力


共 (1) 个答案

  1. # 1 楼答案

    我建议您使用java。木卫一。并将平方和解析操作与文件读取操作分开。 可能是这样的:

    public class MathSquareTester {
    
        public static void main (String[] args) throws IOException {
            List<Integer> squaredNums = new ArrayList<Integer>();
            FileInputStream in = new FileInputStream(file_name);
            BufferedReader reader = new BufferedReader(new FileReader(in));
    
            String line = reader.readLine();
    
            while (line != null) {
                //Assuming each line represents a separate integer
    
                squaredNums.add((int)Math.pow(Integer.parseInt(line)), 2);
                line = reader.readLine();
            }
    
            int[] numsArray = new int[squaredNums.size];
            squaredNums.toArray(numsArray);
            //All of your numbers are now stored in numsArray
        }
    }