本文介绍了如何在Java中创建魔术方块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的任务是编写一个程序,要求用户输入内容,该方法将返回输入内容是否为幻方.无论我输入到控制台中什么,程序都会返回我输入了一个幻方.我想念什么?
my task is to write a program that ask user for an input and the method will return whether or not the input forms a magic square. No matter what I enter into the console, the program returns that I entered a magic square. What am I missing?
魔方定义:如果行,列和对角线的总和相同,则二维数组就是魔方.
Magic square definition: a 2 dimensional array is a magic square if the sum of the rows, columns, and diagonals is the same.
here is the Code:
public class MagicSquares {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Integer> ints = new ArrayList<Integer>();
int current = 0;
do{
System.out.print("Enter an int. Enter -1 when done>");
current = Integer.parseInt(in.nextLine());
}while(current != -1);
int numInputs = ints.size();
int square = (int) Math.sqrt(numInputs);
if(square*square == numInputs){
int[][] intSquare = new int[square][square];
int x = 0;
while(x < numInputs){
for(int y = 0; y < square; ++y){
for(int z = 0; z < square; ++z){
intSquare[y][z] = ints.get(x);
++x;
}
}
}
if(isMagicSquare(intSquare)){
System.out.println("You entered a magic square");
}else{
System.out.println("You did not enter a magic square");
}
}else{
System.out.println("You did not enter a magic square. " +
"You did not even enter a square...");
}
}
private static Boolean isMagicSquare(int[][] array){
int side = array.length;
int magicNum = 0;
for(int x = 0; x < side; ++x){
magicNum =+ array[0][x];
}
int sumX = 0;
int sumY = 0;
int sumD = 0;
for(int x = 0; x > side; ++x){
for (int y = 0; y < side; ++y){
sumX =+ array[x][y];
sumY =+ array[y][x];
}
sumD =+ array[x][x];
if(sumX != magicNum || sumY != magicNum || sumD != magicNum){
return false;
}
}
return true;
}
}
推荐答案
- 您没有保存输入的内容,因此将0x0正方形传递给
isMagicSquare()
. -
isMagicSquare()
的实现在很多方面是错误的.- 条件
x>边
应为x<侧面
. - 仅在计算完成后才需要检查
sumD
. - 在计算它们之前,您必须初始化
sumX
和sumY
. - 您应使用
+ =
而不是= +
来计算总和. - You didn't save what is entered, so 0x0 square is passed to
isMagicSquare()
. - The implementation of
isMagicSquare()
is wrong in many points.- The condition
x > side
should bex < side
. - You have to check
sumD
only after is calculation is finished. - You have to initialize
sumX
andsumY
before calculating them. - You should use
+=
instead of=+
to calculate the sum.
要纠正:
使代码保存输入
do{ System.out.print("Enter an int. Enter -1 when done>"); current = Integer.parseInt(in.nextLine()); if (current != -1) ints.add(current); // add this line to the loop to read the input }while(current != -1);
以及正确的
isMagicSquare()
.private static Boolean isMagicSquare(int[][] array){ int side = array.length; int magicNum = 0; for(int x = 0; x < side; ++x){ magicNum += array[0][x]; } int sumD = 0; for(int x = 0; x < side; ++x){ int sumX = 0; int sumY = 0; for (int y = 0; y < side; ++y){ sumX += array[x][y]; sumY += array[y][x]; } sumD =+ array[x][x]; if(sumX != magicNum || sumY != magicNum){ return false; } } return sumD == magicNum; }
这篇关于如何在Java中创建魔术方块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- The condition
- 条件