测试.txt文件的幻方的Java [英] Testing a .txt file for a Magic Square Java

查看:108
本文介绍了测试.txt文件的幻方的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望要问,但我想不通这个任务,也不可以在TA,当我问的帮助。

我必须采取输入从一个文本文件,喂文件中的整数到一个数组列表,测试,看看它是否为n×n的幻方。 n等于数组列表长度的平方根。如果它不是一个完美的广场立刻失败幻方检验。

反正我已经几乎完成;我似乎就是不明白我的教授告诉/问我们的幻方测试的最后一步做。

这些最后四个步骤之前,所有的测试工作完美无缺。我会在以后的步骤后我目前的code。


  <醇开始=4>
  
  • 让rowSums和colSums是长度为n的两个阵列与项全部为零。此外,让sumDiagMajor和sumDiagMinor,再presenting总和
      沿着条目的左上角到右下角和右上角的
      左下角分别对角线,表的。


  •   
  • 让指数= 0


  •   
  • 重复,直到指数= N2(一)增加rowSums [行]由ArrayList的{}指数(B)增量colSums [COL]由ArrayList的{}指数(C)
      如果row​​ =关口,再由ArrayList的{}指数递增sumDiagMajor。 (d)若
      排+ COL = N-1,然后通过ArrayList的{}指数(E)增加sumDiagMinor
      1增量指数


  •   
  • 如果sumDiagMajor等于sumDiagMinor和rowSums和colSums的每个条目,该表是幻方;否则,它不是


  •   

      INT rowSums [] =新的INT [_n]
                                   INT colSums [] =新的INT [_n]
                                   INT sumDiagMajor = 0;
                                   INT sumDiagMinor = 0;
                                   INT行,列;
                                   排= COL = 0;                               对于(INT指数= 0;指数≤(N * N);指数++)
                                   {                                   rowSums [行] = rowSums [行] + magicSquare.get(指数);
                                       colSums [COL] = colSums [COL] + magicSquare.get(指数);
                                       如果(行==列)
                                       {                                       sumDiagMajor = sumDiagMajor + magicSquare.get(指数);
                                       }                                   如果((行+ COL)==(N - 1))
                                       {                                    sumDiagMinor = sumDiagMinor + magicSquare.get(指数);                                   }
                                   }                               的System.out.println(sumDiagMajor);
                                   的System.out.println(sumDiagMinor);

    我的问题包括,我是不是正确递增阵列rowSums和rowCols?他从来没有真正指出做什么用的行列的,所以它们初始化为零的最佳选择?

    如果我做的一切都是正确的,到目前为止,怎能sumDiagMajor等于sumDiagMinor因为行将始终等于COLS,所以第二个嵌套的if语句将永远不会运行。因此,将排除一切测试作为一个魔方?

    对不起,长的帖子,但这是非常混乱的。


    解决方案

    根据您的更新的要求。一个完整的例子。

     公共静态无效的主要(字串[] args){
            清单&LT;整数GT;幻方= Arrays.asList(2,7,6,9,5,1,4,3,8);        INT N =(INT)的Math.sqrt(magicSquare.size());
            INT rowSums [] =新的INT [n];
            INT colSums [] =新的INT [n];
            INT sumDiagMajor = 0;
            INT sumDiagMinor = 0;        INT排= -1;
            INT COL = -1;        对于(INT指数= 0;指数 - LT; N * N;指数++){            山坳++;
                如果(COL%N == 0){
                    行++;
                    COL = 0;
                }            rowSums [行] = rowSums [行] + magicSquare.get(指数);
                colSums [COL] = colSums [COL] + magicSquare.get(指数);
                如果(行==列)
                {
                    sumDiagMajor + = magicSquare.get(指数);
                }            如果((行+ COL)==(N - 1))
                {
                    sumDiagMinor + = magicSquare.get(指数);
                }        }        布尔isMagicSquare =真;
            的for(int i = 0; I&LT; N&放大器;&安培; isMagicSquare;我++){
                isMagicSquare = sumDiagMajor == rowSums [1] - 放大器;&安培; sumDiagMajor == colSums [I]
            }
            isMagicSquare = isMagicSquare&放大器;&安培; sumDiagMajor == sumDiagMinor;        的System.out.println(isMagicSquare); //真
        }

    I didn't want to have to ask, but I can not figure out this assignment, and neither could the TA when I asked for help.

    I have to take input from a text file, feed the integers in the file into an array list, and test to see if it is a n x n magic square. n is equal to the square root of the array list's length. If it isn't a perfect square it immediately fails the magic square test.

    Anyway I have it almost completed; I just don't seem to understand what my professor is telling/asking us to do in the final step of the magic square test.

    All the testing before these final four steps works flawlessly. I'll post my current code after the steps.

    1. Let rowSums and colSums be two arrays of length n with entries all zeros. Also, let sumDiagMajor and sumDiagMinor, representing the sum of the entries along the top-left to bottom-right and top-right to bottom-left diagonals, respectively, of the table.

    2. Let index = 0

    3. Repeat until index = n2 (a) Increment rowSums[row] by ArrayList{index} (b) increment colSums[col] by ArrayList{index} (c) If row = col, then increment sumDiagMajor by ArrayList{index}. (d) If row + col = n−1, then increment sumDiagMinor by ArrayList{index} (e) Increment index by 1

    4. If sumDiagMajor is equal to sumDiagMinor and each entry of rowSums and colSums, the the table is a magic square; otherwise, it is not.

                                   int rowSums[] = new int[_n];
                                   int colSums[] = new int[_n];
                                   int sumDiagMajor = 0;
                                   int sumDiagMinor = 0;
    
    
                                   int row, col;
                                   row = col = 0;
    
                                   for (int index = 0; index < (n*n); index++)
                                   {
    
    
    
                                       rowSums[row] = rowSums[row] + magicSquare.get(index);
                                       colSums[col] = colSums[col] + magicSquare.get(index);
    
    
                                       if (row == col)
                                       {
    
                                           sumDiagMajor = sumDiagMajor + magicSquare.get(index);
    
    
                                       }
    
                                       if ((row + col) == (n - 1))
                                       {
    
                                        sumDiagMinor = sumDiagMinor + magicSquare.get(index);   
    
                                       }
    
    
                                   }
    
                                   System.out.println(sumDiagMajor);
                                   System.out.println(sumDiagMinor);
    

    My questions include, am I properly incrementing the arrays rowSums, and rowCols? He never actually states what to do with rows or cols, so is initializing them to zero the best option?

    If I did everything correct so far, how can sumDiagMajor ever equal sumDiagMinor because rows will always equal cols, so the second nested if statement will never run. Therefore it will rule out everything test as being a magic square?

    Sorry for the long post, but this is very confusing.

    解决方案

    As per your updated requirements. A full example.

       public static void main(String[] args) {
            List<Integer> magicSquare = Arrays.asList(2,7,6,9,5,1,4,3,8);
    
            int n = (int) Math.sqrt(magicSquare.size());
            int rowSums[] = new int[n];
            int colSums[] = new int[n];
            int sumDiagMajor = 0;
            int sumDiagMinor = 0;
    
            int row = -1;
            int col = -1;
    
            for (int index = 0; index < n*n; index++) {
    
                col++;
                if (col % n == 0) {
                    row++;
                    col = 0;
                }
    
                rowSums[row] = rowSums[row] + magicSquare.get(index);
                colSums[col] = colSums[col] + magicSquare.get(index);
    
    
                if (row == col)
                {
                    sumDiagMajor += magicSquare.get(index);
                }
    
                if ((row + col) == (n - 1))
                {
                    sumDiagMinor += magicSquare.get(index);
                }
    
            }
    
            boolean isMagicSquare = true;
            for (int i = 0; i < n && isMagicSquare; i++) {
                isMagicSquare = sumDiagMajor == rowSums[i] && sumDiagMajor == colSums[i];
            }
            isMagicSquare = isMagicSquare && sumDiagMajor == sumDiagMinor;
    
            System.out.println(isMagicSquare); // true
        }
    

    这篇关于测试.txt文件的幻方的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆