创建幻方(java)

lx0bsm1f  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(290)

我有点纠结于这个方阵编码项目该怎么办。
每当我尝试输入任何值时,结果总是正确的,方阵就是一个幻方。例如,事实证明:

  1. 16 03 02 13
  2. 05 10 11 08
  3. 09 06 07 12
  4. 04 15 14 01

但当我输入如下值时:

  1. 03 04 16 02
  2. 05 01 02 10
  3. 05 08 07 12
  4. 03 14 13 09

这应该返回假,但它仍然返回真说,这是一个魔方。
要求是我需要所有的方法
“public void add(int i,int row,int col)”:将整数添加到指定位置的矩阵中。
公众的
“public boolean allinrange”:确定矩阵中的所有值是否在适当的范围内
“public boolean allunique”:确定矩阵中的所有值是否只出现一次
“public boolean ismagic”:确定矩阵是否表示幻方。这意味着:
用户为某些数字n输入了n^2个数字
数字仅介于1和n^2之间(包括1和n^2)
每个数字在矩阵中正好出现一次
每行、每列和两条对角线中的元素之和相等
公共类平方矩阵{

  1. private int[][] array;
  2. public SquareMatrix(int size)
  3. {
  4. array = new int[size][size];
  5. }
  6. public void add(int i, int row, int column) {array[row][column] = i;}
  7. //Just checks if the #of rows & columns are between 1-n^2
  8. public boolean allInRange()
  9. {
  10. int n = array.length;
  11. for (int row = 0; row < n; row++)
  12. {
  13. for (int col = 0; col < array[row].length; col++)
  14. {
  15. if (array[row][col] < 1 || array[row][col] > n*n)
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. public boolean allUnique()
  22. {
  23. for (int i =0; i < array.length - 1; i++)
  24. {
  25. for (int j = i + 1; j < array.length; j++)
  26. {
  27. if(array[i]==array[j])
  28. return false;
  29. }
  30. }
  31. return true;
  32. }
  33. //Supposed to call the other methods (allInRange & allUnique)
  34. public boolean isMagic()
  35. {
  36. for(int[] row : array)
  37. {
  38. for (int num : row)
  39. {
  40. if (num == 0)
  41. return false;
  42. }
  43. }
  44. boolean range = allInRange();
  45. if (range == true)
  46. return true;
  47. if (range == false)
  48. return false;
  49. boolean unique = allUnique();
  50. if (unique == true)
  51. return true;
  52. if (unique == false)
  53. return false;
  54. int sumRow;
  55. int sumCol;
  56. int sum1 = 0;
  57. int sum2 = 0;
  58. //Sum of Left to Right Diaganol
  59. for (int i = 0; i < array.length; i++)
  60. {
  61. sum1 += array[i][i];
  62. }
  63. //sum of right to left diaganol
  64. for (int j = 0; j < array.length; j++)
  65. {
  66. sum2 += array[j][array.length-1-j];
  67. }
  68. if (sum1 != sum2)
  69. return false;
  70. //Sum of Rows
  71. for (int row = 0; row < array.length; row++)
  72. {
  73. sumRow = 0;
  74. for (int col = 0; col < array[row].length; col++)
  75. sumRow += array[row][col];
  76. if (sumRow != sum1)
  77. return false;
  78. }
  79. //Sum of Col
  80. for (int i = 0; i < array.length; i++)
  81. {
  82. sumCol = 0;
  83. for (int j = 0; j < array.length; j++)
  84. sumCol = array[j][i];
  85. if (sumCol != sum1)
  86. return false;
  87. }
  88. return true;
  89. }
  90. public String toString()
  91. {
  92. int n = array.length;
  93. String lol = "";
  94. for (int[] row : array)
  95. {
  96. for (int num : row)
  97. {
  98. String hi = String.format("%0"+(n*n+"").length()+"d",num);
  99. lol += hi + " ";
  100. }
  101. lol += "\n";
  102. }
  103. return lol;
  104. }

}
这是我的驾驶课

  1. import javax.swing.*;
  2. public class SquareMatrixDriver {
  3. public static void main(String[] args) { //My favorite line in history
  4. JFrame bot = new JFrame(); //We can use JFrame to read the inputs
  5. do
  6. {
  7. //We have to make sure that it is a valid input or else I am doomed
  8. int size = 0;
  9. do
  10. {
  11. size = Integer.parseInt(JOptionPane.showInputDialog(bot, "Enter the size of the matrix."));
  12. if (size < 1)
  13. {
  14. JOptionPane.showMessageDialog(bot, "Invalid size! Enter a number greater than 0.");
  15. }
  16. }
  17. while(size < 1);
  18. SquareMatrix matrix = new SquareMatrix(size);
  19. for (int i=0; i<size; i++)
  20. {
  21. //Gets thhe User's Input
  22. String[] stringInput;
  23. do
  24. {
  25. stringInput = JOptionPane.showInputDialog(bot, "Enter the row number" + (i + 1) + ", with " + size + " elements, split by commas.").split(",");
  26. if (stringInput.length != size)
  27. { //In this code we basically enter the numbers with commas
  28. JOptionPane.showMessageDialog(bot, "Invalid size! " + stringInput.length + " elements entered but " + size + " required.");
  29. }
  30. }
  31. while(stringInput.length != size);
  32. int[] intInput = new int[size];
  33. for (int o=0; o<size; o++)
  34. {
  35. }
  36. for (int o=0; o<size; o++)
  37. {
  38. matrix.add(Integer.parseInt(stringInput[o]), i, o); //Here we would put everything into the Matrix
  39. }
  40. }
  41. JOptionPane.showMessageDialog(bot, "The matrix is " + (matrix.isMagic()? "very" : "not") + " correct"); //This line will output if the Matrix works or doesnt work
  42. JOptionPane.showMessageDialog(bot, matrix); // Enters out the final output
  43. } while (JOptionPane.showConfirmDialog(bot, "Do you wish to exit?", "Exit", JOptionPane.YES_NO_OPTION) == 1); //Asks the User if they would like to exit the program
  44. }
  45. }
gzjq41n4

gzjq41n41#

我可以通过目视检查发现的错误:
allunique()是完全错误的,您必须检查每个数字在矩阵中是否只出现一次,但是您比较的是完全不同的行数组,检查唯一性的最佳方法通常是使用哈希集,但是由于这里有一个非常明确的数字范围(从1到n2),因此可以使用任意n2布尔数组。扫描方阵并测试/设置数组中的相应元素(如果已设置),返回false。

  1. public boolean allUnique() {
  2. int n=array.length;
  3. boolean[] set=new boolean[n*n];
  4. for (int i=0; i < n; i++) {
  5. for (int j=0;j < n; j++) {
  6. //Here assuming that you already sucessfully called allInRange,
  7. //otherwise we must check bounds to avoid an index out of bounds exception
  8. if(set[array[i][j]-1]) {
  9. return false;
  10. }
  11. set[array[i][j]-1] = true;
  12. }
  13. }
  14. return true;
  15. }

在方法ismagic()中,所有这部分都是错误和冗余的

  1. boolean range = allInRange();
  2. if (range == true)
  3. return true; //WRONG, this will immediately return with true, without further checks
  4. if (range == false)
  5. return false;
  6. boolean unique = allUnique();
  7. if (unique == true)
  8. return true; //WRONG, same as before
  9. if (unique == false)
  10. return false;

把它换成

  1. if (!allInRange()) {
  2. return false;
  3. }
  4. if (!allUnique()) {
  5. return false;
  6. }

最后,在ismagic()中,当计算列的和时,缺少加法

  1. sumCol = array[j][i];

必须替换为

  1. sumCol += array[j][i];
展开查看全部

相关问题