Java奇数幻方生成程序和三阶幻方检查程序

public class MagicSquareGen {

	public static void main(String[] args) {
		int n = 5;
		if (n % 2 == 0)
			throw new RuntimeException("n must be odd");

		int[][] magic = new int[n][n];

		int row = n - 1;
		int col = n / 2;
		magic[row][col] = 1;

		for (int i = 2; i <= n * n; i++) {
			if (magic[(row + 1) % n][(col + 1) % n] == 0) {
				row = (row + 1) % n;
				col = (col + 1) % n;
			} else {
				row = (row - 1 + n) % n;
				// don't change col
			}
			magic[row][col] = i;
		}

		// print results
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (magic[i][j] < 10)
					System.out.print(" "); // for alignment
				if (magic[i][j] < 100)
					System.out.print(" "); // for alignment
				System.out.print(magic[i][j] + " ");
			}
			System.out.println();
		}

	}
}

//importing Scanner class
import java.util.Scanner;

//importing Array tools
import java.util.Arrays;

//importing packages used for detecting key events
import java.awt.Toolkit;

/**
 *
 * @author User
 */
public class MagicSquareCheck {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		// TODO code application logic here

		// a strings type variable used to store text
		String textvar = "";
		// flag which determines whether sqr array is filled with numbers
		boolean existingsqr = false;
		// array for storing magic square
		int[][] sqr = new int[3][3];
		// used in for cycles
		int sqrsize = sqr.length;
		// creating new scanner object which takes InputStream as parameter
		Scanner in = new Scanner(System.in);

		do {
			System.out.println("Input \"new\" to start filling new 3x3 square");
			if (existingsqr)
				System.out.println("Input \"show\" to display existing square");
			if (existingsqr)
				System.out.println("Input \"info\" to display info about existing square");
			System.out.println("Input \"quit\" for exiting the application");

			textvar = in.nextLine();
			if ("new".equals(textvar)) {
				System.out.println("Input 9 numbers ");

				for (int i = 0; i < sqrsize; i++) {
					for (int j = 0; j < sqrsize; j++) {
						System.out.println("Input value for row " + (i + 1) + " column " + (j + 1));
						sqr[i][j] = in.nextInt();
					}
				}
				existingsqr = true;
				System.out.println("Thanks!");
				System.out.println();
			} else if ("show".equals(textvar) && existingsqr) {
				System.out.println("Current square:");
				System.out.println(sqr[0][0] + " " + sqr[0][1] + " " + sqr[0][2]);
				System.out.println(sqr[1][0] + " " + sqr[1][1] + " " + sqr[1][2]);
				System.out.println(sqr[2][0] + " " + sqr[2][1] + " " + sqr[2][2]);
			} else if ("info".equals(textvar) && existingsqr) {
				int[] sums_rows = new int[sqrsize];
				Arrays.fill(sums_rows, 0);

				int[] sums_columns = new int[sqrsize];
				Arrays.fill(sums_columns, 0);

				int[] sums_diagonals = new int[2];
				Arrays.fill(sums_diagonals, 0);

				for (int i = 0; i < sqrsize; i++) {
					for (int j = 0; j < sqrsize; j++) {
						sums_rows[i] += sqr[i][j];
					}
					System.out.println("the sum of elements of row " + (i + 1) + " :\n" + sums_rows[i]);
				}
				for (int j = 0; j < sqrsize; j++) {
					for (int i = 0; i < sqrsize; i++) {
						sums_columns[j] += sqr[i][j];
					}
					System.out.println("the sum of elements of column " + (j + 1) + " :\n" + sums_columns[j]);
				}

				for (int i = 0; i < sqrsize; i++) {
					sums_diagonals[0] += sqr[i][i];
				}
				System.out.println("the sum of diagonal 1 " + "\n" + sums_diagonals[0]);

				for (int i = 0; i < sqrsize; i++) {
					sums_diagonals[1] += sqr[i][(sqrsize - 1) - i];
				}
				System.out.println("the sum of diagonal 2 " + "\n" + sums_diagonals[1]);

				boolean isMagicSquare = true;

				int comparedSum = sums_rows[0];
				for (int i = 1; i < sqrsize; i++) {
					isMagicSquare = isMagicSquare && (comparedSum == sums_rows[i]);
				}
				for (int i = 0; i < sqrsize; i++) {
					isMagicSquare = isMagicSquare && (comparedSum == sums_columns[i]);
				}
				for (int i = 0; i < 2; i++) {
					isMagicSquare = isMagicSquare && (comparedSum == sums_diagonals[i]);
				}

				if (isMagicSquare)
					System.out.println("All sums are equal, this is a magic square");
				else
					System.out.println("The sums are not equal, so this isn't a magic square");

				System.out.println();
			} else if (!"quit".equals(textvar)) {
				// cleans screen by pushing text off screen
				for (int clear = 0; clear < 1000; clear++) {
					System.out.println("\b");
				}
			}
		} while (!"quit".equals(textvar));
		// loop will repeat until textvar reaches the necessary value
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值