Magic Square Java阅读代码顶部的描述
/**我正在使用二维数组创建一个完美的正方形。在程序中,每行、每列和每对角线必须彼此相等。我需要有关我的checkArrays方法的帮助。我的checkArray方法的目的是检查每个数组是否彼此相等,如果一个数组彼此不相等,则它不是一个完美的正方形(返回false)*/ 公共级完美广场{Magic Square Java阅读代码顶部的描述,java,Java,/**我正在使用二维数组创建一个完美的正方形。在程序中,每行、每列和每对角线必须彼此相等。我需要有关我的checkArrays方法的帮助。我的checkArray方法的目的是检查每个数组是否彼此相等,如果一个数组彼此不相等,则它不是一个完美的正方形(返回false)*/ 公共级完美广场{ static Scanner sc; static int[][] magicS; /** * Builds perfect square */ public static void buildSquar
static Scanner sc;
static int[][] magicS;
/**
* Builds perfect square
*/
public static void buildSquare() {
magicS = new int[3][3];
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
//asks the user to input a number.
System.out.println("Please type number for column " + (r + 1) + " and row " + (c + 1));
magicS[r][c] = sc.nextInt();
}
}
}
/**
* Prints perfect square
*/
public static void printSquare() {
for (int r = 0; r < 3; r += 1) {
for (int c = 0; c < 3; c += 1) {
System.out.print(magicS[r][c] + " ");
}
System.out.println("");
}
}
/**
* Adds each row together
*
* @param r
* @return
*/
public static int getRowSums(int r) {
int sum = 0;
for (int c = 0; r < magicS[r].length; c++) {
sum += magicS[r][c];
}
return sum;
}
/**
* Adds each colum together
*
* @param c
* @return
*/
public static int getColSums(int c) {
int sum = 0;
for (int r = 0; r < magicS.length; r++) {
sum += magicS[r][c];
}
return sum;
}
/**
* Adds each diagonal together
*
* @return
*/
public static int[] getDiaganolSums() {
int[] sums = new int[2];
for (int d = 0; d < magicS.length; d++) {
sums[0] += magicS[d][d];
sums[1] += magicS[d][magicS.length - 1 - d];
}
return sums;
}
/**
* Checks to see if the sum of each row, colum, and diagonal should all be
* the same, If perfect returns perfect.
*
* @return
*/
public static boolean checkValid() {
int[] rowSums = new int[magicS.length];
for (int r = 0; r < magicS.length; r++) {
rowSums[r] = getRowSums(r);
}
int[] colsSums = new int[magicS.length];
for (int c = 0; c < magicS.length; c++) {
colsSums[c] = getColSums(c);
}
int[] diagSums = getDiaganolSums();
boolean perfect = checkArrays(rowSums, colsSums, diagSums);
return perfect;
}
/**
* checks if each row, colum, and diagonal are equal to each other
*
* @param r
* @param c
* @param d
* @return
*/
public static boolean checkArrays(int[] r, int[] c, int[] d) {
}
/**
* The main method asks the user to input numbers that correlate to a row
* and column When run, a 3 x 3 array of the numbers inputted will be printed.
*
* @param args
*/
public static void main(String[] args) {
//allows to get info from user
sc = new Scanner(System.in);
//executes the buildSquare method
buildSquare();
//executes the printSquare method
printSquare();
//executes the checkValid method
checkValid();
}
静态扫描sc;
静态int[][]magicS;
/**
*建造完美的正方形
*/
公共静态void buildSquare(){
magicS=新整数[3][3];
对于(int r=0;r<3;r++){
对于(int c=0;c<3;c++){
//要求用户输入一个数字。
System.out.println(“请为“+(r+1)+”列和“+(c+1)行键入编号”);
magicS[r][c]=sc.nextInt();
}
}
}
/**
*打印出完美的正方形
*/
公共静态void printSquare(){
对于(int r=0;r<3;r+=1){
对于(int c=0;c<3;c+=1){
系统输出打印(magicS[r][c]+“”);
}
System.out.println(“”);
}
}
/**
*将每行相加
*
*@param r
*@返回
*/
公共静态int getRowSums(int r){
整数和=0;
对于(int c=0;r
}你的代码有什么问题?@Berger问题是
checkArrays
返回一个布尔值,但没有代码,因此代码无法编译。所以用代码填充它。John,有很多人愿意帮助你。但是,给你真正需要的帮助要比为你编写方法容易得多。这对你也有好处。首先在代码中用英语注释,说明您计划对输入做什么,以及如何将其转换为输出。然后编写执行此操作的代码。然后我们可以协助调试。欢迎使用stackoverflow,我想您需要添加一个return语句来解决这个问题。最好是陈述问题,使事情变得容易些。