📄 grid.java
字号:
// Name: Tsang Kai Ming, Patrick (25)
// Couse Code 41300/1 Group V
// Subject: ICT-1414 Object-oriented Programming
// Sudoku Assignment 2 File (Java Programming)
import java.util.Arrays;
class Grid {
//------------------------------------------------------------------------------------------------
public final static int AB = 3;
private int[][] numbers=new int[ AB ][AB];
// TODO:
// Declare a 3x3 2-dimension integer array
// Initialize your array to all zero in your default constructor
public Grid() {
//------------------------------------------------------------------------------------------------
for (int i = 0; i < AB; i++) {
for (int j = 0; j < AB; j++) {
numbers[i][j] =0;
}
}
}
// Store the numbers into your integer array in this constructor
public Grid(int[][] numbers) {
for (int x = 0; x<3;x++)
for (int y = 0; y<3;y++)
this.numbers[x][y] = numbers[x][y];
}
// Assign the number to the appropriate place in your array
public void put(int num, int x, int y) {
//------------------------------------------------------------------------------------------------
if (num<0 || num>9 || x<0 || x>2 || y<0 || y>2){
System.out.println("incorrect number, you should input 0-9");}
else{
this.numbers[x][y]=num;
}
}
// Return the number from the appropriate place in the array
public int get(int x, int y) {
//------------------------------------------------------------------------------------------------
return this.numbers[x][y];
}
// Check if num is valid in your array
// If num appears more than once in the array, it returns false
// otherwise, returns true
/*** HINTS: Use a nested for loop to count the appearance of num ***/
public boolean checkInside(int num) {
//------------------------------------------------------------------------------------------------
int temp = 0;
for (int x = 0;x<3;x++)
for (int y = 0;y<3;y++)
if (get(x,y)==num){
temp += 1;
if (temp >= 2)
return false;
}
return true;
}
// Return a String representing the Grid
// e.g.
//
// 3 5 7
// 2 4 6
// 1 8 9
public String toString() {
//------------------------------------------------------------------------------------------------
String str="";
for(int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
str+=get(i,j)+" ";
// if it is a zero, print a space " " instead
}
str+="\n";
}
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -