创建幻方 (Java)
Creating a magic square (Java)
我有点不知道如何处理这个方阵编码项目。
每当我尝试输入任何值时,结果总是为真,方阵是一个幻方。例如,结果会是这样:
16 03 02 13
05 10 11 08
09 06 07 12
04 15 14 01
但是当我输入如下值时:
03 04 16 02
05 01 02 10
05 08 07 12
03 14 13 09
这应该 return 错误,但它仍然 return 正确地说它是一个幻方。
要求是我需要所有的方法
"public void add(int i, int row, int col)": 在矩阵的指定位置添加一个整数。
public
public boolean allInRange:判断矩阵中的所有值是否都在合适的范围内
public boolean allUnique:判断矩阵中的所有值是否只出现一次
"public boolean isMagic":判断矩阵是否说明幻方。这意味着:
用户输入了 n^2 个号码,对应号码 n
数字只能在1到n^2之间,包括在内
每个数字在矩阵中只出现一次
每行、每列、两条对角线的元素之和相等
public class 方阵 {
private int[][] array;
public SquareMatrix(int size)
{
array = new int[size][size];
}
public void add(int i, int row, int column) {array[row][column] = i;}
//Just checks if the #of rows & columns are between 1-n^2
public boolean allInRange()
{
int n = array.length;
for (int row = 0; row < n; row++)
{
for (int col = 0; col < array[row].length; col++)
{
if (array[row][col] < 1 || array[row][col] > n*n)
return false;
}
}
return true;
}
public boolean allUnique()
{
for (int i =0; i < array.length - 1; i++)
{
for (int j = i + 1; j < array.length; j++)
{
if(array[i]==array[j])
return false;
}
}
return true;
}
//Supposed to call the other methods (allInRange & allUnique)
public boolean isMagic()
{
for(int[] row : array)
{
for (int num : row)
{
if (num == 0)
return false;
}
}
boolean range = allInRange();
if (range == true)
return true;
if (range == false)
return false;
boolean unique = allUnique();
if (unique == true)
return true;
if (unique == false)
return false;
int sumRow;
int sumCol;
int sum1 = 0;
int sum2 = 0;
//Sum of Left to Right Diaganol
for (int i = 0; i < array.length; i++)
{
sum1 += array[i][i];
}
//sum of right to left diaganol
for (int j = 0; j < array.length; j++)
{
sum2 += array[j][array.length-1-j];
}
if (sum1 != sum2)
return false;
//Sum of Rows
for (int row = 0; row < array.length; row++)
{
sumRow = 0;
for (int col = 0; col < array[row].length; col++)
sumRow += array[row][col];
if (sumRow != sum1)
return false;
}
//Sum of Col
for (int i = 0; i < array.length; i++)
{
sumCol = 0;
for (int j = 0; j < array.length; j++)
sumCol = array[j][i];
if (sumCol != sum1)
return false;
}
return true;
}
public String toString()
{
int n = array.length;
String lol = "";
for (int[] row : array)
{
for (int num : row)
{
String hi = String.format("%0"+(n*n+"").length()+"d",num);
lol += hi + " ";
}
lol += "\n";
}
return lol;
}
}
这是我的driverclass
import javax.swing.*;
public class SquareMatrixDriver {
public static void main(String[] args) { //My favorite line in history
JFrame bot = new JFrame(); //We can use JFrame to read the inputs
do
{
//We have to make sure that it is a valid input or else I am doomed
int size = 0;
do
{
size = Integer.parseInt(JOptionPane.showInputDialog(bot, "Enter the size of the matrix."));
if (size < 1)
{
JOptionPane.showMessageDialog(bot, "Invalid size! Enter a number greater than 0.");
}
}
while(size < 1);
SquareMatrix matrix = new SquareMatrix(size);
for (int i=0; i<size; i++)
{
//Gets thhe User's Input
String[] stringInput;
do
{
stringInput = JOptionPane.showInputDialog(bot, "Enter the row number" + (i + 1) + ", with " + size + " elements, split by commas.").split(",");
if (stringInput.length != size)
{ //In this code we basically enter the numbers with commas
JOptionPane.showMessageDialog(bot, "Invalid size! " + stringInput.length + " elements entered but " + size + " required.");
}
}
while(stringInput.length != size);
int[] intInput = new int[size];
for (int o=0; o<size; o++)
{
}
for (int o=0; o<size; o++)
{
matrix.add(Integer.parseInt(stringInput[o]), i, o); //Here we would put everything into the Matrix
}
}
JOptionPane.showMessageDialog(bot, "The matrix is " + (matrix.isMagic()? "very" : "not") + " correct"); //This line will output if the Matrix works or doesnt work
JOptionPane.showMessageDialog(bot, matrix); // Enters out the final output
} 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
}
}
我可以通过目测发现的错误:
allUnique() 是完全错误的,你必须检查每个数字是否只在矩阵中出现一次,但是你正在比较完全不同的行数组,检查唯一性的最好方法通常是使用hashset,但是因为这里你有一个非常定义的数字范围(从 1 到 n2),然后使用 n2 布尔值的任何数组。扫描方阵和test/set数组中对应的元素,如果已经设置returnfalse.
public boolean allUnique() {
int n=array.length;
boolean[] set=new boolean[n*n];
for (int i=0; i < n; i++) {
for (int j=0;j < n; j++) {
//Here assuming that you already sucessfully called allInRange,
//otherwise we must check bounds to avoid an index out of bounds exception
if(set[array[i][j]-1]) {
return false;
}
set[array[i][j]-1] = true;
}
}
return true;
}
在 isMagic() 方法中,这部分都是错误和多余的
boolean range = allInRange();
if (range == true)
return true; //WRONG, this will immediately return with true, without further checks
if (range == false)
return false;
boolean unique = allUnique();
if (unique == true)
return true; //WRONG, same as before
if (unique == false)
return false;
直接替换为
if (!allInRange()) {
return false;
}
if (!allUnique()) {
return false;
}
最后在 isMagic() 中计算列的总和时,缺少加法
sumCol = array[j][i];
必须替换为
sumCol += array[j][i];
我有点不知道如何处理这个方阵编码项目。
每当我尝试输入任何值时,结果总是为真,方阵是一个幻方。例如,结果会是这样:
16 03 02 13
05 10 11 08
09 06 07 12
04 15 14 01
但是当我输入如下值时:
03 04 16 02
05 01 02 10
05 08 07 12
03 14 13 09
这应该 return 错误,但它仍然 return 正确地说它是一个幻方。
要求是我需要所有的方法
"public void add(int i, int row, int col)": 在矩阵的指定位置添加一个整数。
public
public boolean allInRange:判断矩阵中的所有值是否都在合适的范围内
public boolean allUnique:判断矩阵中的所有值是否只出现一次
"public boolean isMagic":判断矩阵是否说明幻方。这意味着:
用户输入了 n^2 个号码,对应号码 n
数字只能在1到n^2之间,包括在内
每个数字在矩阵中只出现一次
每行、每列、两条对角线的元素之和相等
public class 方阵 {
private int[][] array; public SquareMatrix(int size) { array = new int[size][size]; } public void add(int i, int row, int column) {array[row][column] = i;} //Just checks if the #of rows & columns are between 1-n^2 public boolean allInRange() { int n = array.length; for (int row = 0; row < n; row++) { for (int col = 0; col < array[row].length; col++) { if (array[row][col] < 1 || array[row][col] > n*n) return false; } } return true; } public boolean allUnique() { for (int i =0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if(array[i]==array[j]) return false; } } return true; } //Supposed to call the other methods (allInRange & allUnique) public boolean isMagic() { for(int[] row : array) { for (int num : row) { if (num == 0) return false; } } boolean range = allInRange(); if (range == true) return true; if (range == false) return false; boolean unique = allUnique(); if (unique == true) return true; if (unique == false) return false; int sumRow; int sumCol; int sum1 = 0; int sum2 = 0; //Sum of Left to Right Diaganol for (int i = 0; i < array.length; i++) { sum1 += array[i][i]; } //sum of right to left diaganol for (int j = 0; j < array.length; j++) { sum2 += array[j][array.length-1-j]; } if (sum1 != sum2) return false; //Sum of Rows for (int row = 0; row < array.length; row++) { sumRow = 0; for (int col = 0; col < array[row].length; col++) sumRow += array[row][col]; if (sumRow != sum1) return false; } //Sum of Col for (int i = 0; i < array.length; i++) { sumCol = 0; for (int j = 0; j < array.length; j++) sumCol = array[j][i]; if (sumCol != sum1) return false; } return true; } public String toString() { int n = array.length; String lol = ""; for (int[] row : array) { for (int num : row) { String hi = String.format("%0"+(n*n+"").length()+"d",num); lol += hi + " "; } lol += "\n"; } return lol; }
}
这是我的driverclass
import javax.swing.*;
public class SquareMatrixDriver {
public static void main(String[] args) { //My favorite line in history
JFrame bot = new JFrame(); //We can use JFrame to read the inputs
do
{
//We have to make sure that it is a valid input or else I am doomed
int size = 0;
do
{
size = Integer.parseInt(JOptionPane.showInputDialog(bot, "Enter the size of the matrix."));
if (size < 1)
{
JOptionPane.showMessageDialog(bot, "Invalid size! Enter a number greater than 0.");
}
}
while(size < 1);
SquareMatrix matrix = new SquareMatrix(size);
for (int i=0; i<size; i++)
{
//Gets thhe User's Input
String[] stringInput;
do
{
stringInput = JOptionPane.showInputDialog(bot, "Enter the row number" + (i + 1) + ", with " + size + " elements, split by commas.").split(",");
if (stringInput.length != size)
{ //In this code we basically enter the numbers with commas
JOptionPane.showMessageDialog(bot, "Invalid size! " + stringInput.length + " elements entered but " + size + " required.");
}
}
while(stringInput.length != size);
int[] intInput = new int[size];
for (int o=0; o<size; o++)
{
}
for (int o=0; o<size; o++)
{
matrix.add(Integer.parseInt(stringInput[o]), i, o); //Here we would put everything into the Matrix
}
}
JOptionPane.showMessageDialog(bot, "The matrix is " + (matrix.isMagic()? "very" : "not") + " correct"); //This line will output if the Matrix works or doesnt work
JOptionPane.showMessageDialog(bot, matrix); // Enters out the final output
} 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
}
}
我可以通过目测发现的错误:
allUnique() 是完全错误的,你必须检查每个数字是否只在矩阵中出现一次,但是你正在比较完全不同的行数组,检查唯一性的最好方法通常是使用hashset,但是因为这里你有一个非常定义的数字范围(从 1 到 n2),然后使用 n2 布尔值的任何数组。扫描方阵和test/set数组中对应的元素,如果已经设置returnfalse.
public boolean allUnique() {
int n=array.length;
boolean[] set=new boolean[n*n];
for (int i=0; i < n; i++) {
for (int j=0;j < n; j++) {
//Here assuming that you already sucessfully called allInRange,
//otherwise we must check bounds to avoid an index out of bounds exception
if(set[array[i][j]-1]) {
return false;
}
set[array[i][j]-1] = true;
}
}
return true;
}
在 isMagic() 方法中,这部分都是错误和多余的
boolean range = allInRange();
if (range == true)
return true; //WRONG, this will immediately return with true, without further checks
if (range == false)
return false;
boolean unique = allUnique();
if (unique == true)
return true; //WRONG, same as before
if (unique == false)
return false;
直接替换为
if (!allInRange()) {
return false;
}
if (!allUnique()) {
return false;
}
最后在 isMagic() 中计算列的总和时,缺少加法
sumCol = array[j][i];
必须替换为
sumCol += array[j][i];