?? magic.java
字號:
// Lab 4, Exercise 3
// Magic square
import java.util.*;
public class Magic {
public int SIZE;
private int [] magicSquare;
public Magic (int size)// constructor
{
SIZE = size;
magicSquare = new int[SIZE+1];
for (int i=1; i <=SIZE; i++) magicSquare[i] = 0;
}
// generate a new square
public void newSquare()
{
int i, j, k;
boolean repeat;
magicSquare[1] = 1 + (int) ( Math.random() * SIZE );
for (i=2; i <= SIZE; i++)
{
do {
repeat = false;
k= 1 + (int) ( Math.random() * SIZE );
magicSquare[i] = k;
} while ( repeat(1, i, k ) );
}
}// end newSquare
public void printSquare()
{
for (int i=1; i<=SIZE; i++)
{
if (i%3 == 1 ) System.out.println();
System.out.print(magicSquare[i] +" ");
}
}
//return true if newNumber already exists
public boolean repeat (int first, int current, int newNumber)
{
boolean again = false;
for (int i = first; i < current; i++)
if ( magicSquare[i] == newNumber)
again = true;
return again;
}
public boolean testMagic( )
{
boolean itIsMagic = false;
if ((magicSquare[1] + magicSquare[2] + magicSquare[3]== 15) &&
(magicSquare[4] + magicSquare[5] + magicSquare[6]== 15) &&
(magicSquare[7] + magicSquare[8] + magicSquare[9]== 15) &&
(magicSquare[1] + magicSquare[5] + magicSquare[9]== 15) &&
(magicSquare[7] + magicSquare[5] + magicSquare[3]== 15) )
itIsMagic = true;
return itIsMagic;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -