public class Test2 {
public static void main(String args []){
//Define Variable
Scanner scan=new Scanner(System.in);
final int SIZE=6;
int[][] num=new int[SIZE][SIZE];
//Prompt for input
System.out.println("Enter the initial Grid: ");
for(int i = 0; i < num.length; i++){
for(int j = 0; j < 6; j++){
num[j]=scan.nextInt();
}
}
//Display Before
System.out.println("\n"+"Before:");
for(int i = num.length-1; i >= 0; i--){
System.out.print(i+": ");
for(int j = 0; j < SIZE; j++){
System.out.print(num[j] +" ");
}
System.out.println();
}
System.out.print(" ");
for(int k = 1; k <= 11; k++){System.out.print("=");}
System.out.print("\n"+" ");
for(int k = 0; k <= 5; k++){System.out.print(k+" ");}
System.out.println("\n");
//Display After for(int i = num.length-1; i >= 0; i--){
System.out.print(i+": ");
for(int j = 0; j < SIZE; j++){
..................................................
.................这部份怎完成.....................
..................................................
//印X
if(num[i][j] == -1){
System.out.print("X ");
continue;
}else{
System.out.print(num[i][j]+" ");
}
}
System.out.println();
}
System.out.print(" ");
for(int k = 1; k <= 11; k++){System.out.print("=");}
System.out.print("\n"+" ");
for(int k = 0; k <= 5; k++){System.out.print(k+" ");}
System.out.println("\n");
}
}
注:不使用类方法 ,还不懂
public static void main(String args []){
//Define Variable
Scanner scan=new Scanner(System.in);
final int SIZE=6;
int[][] num=new int[SIZE][SIZE];
//Prompt for input
System.out.println("Enter the initial Grid: ");
for(int i = 0; i < num.length; i++){
for(int j = 0; j < 6; j++){
num[j]=scan.nextInt();
}
}
//Display Before
System.out.println("\n"+"Before:");
for(int i = num.length-1; i >= 0; i--){
System.out.print(i+": ");
for(int j = 0; j < SIZE; j++){
System.out.print(num[j] +" ");
}
System.out.println();
}
System.out.print(" ");
for(int k = 1; k <= 11; k++){System.out.print("=");}
System.out.print("\n"+" ");
for(int k = 0; k <= 5; k++){System.out.print(k+" ");}
System.out.println("\n");
//Display After for(int i = num.length-1; i >= 0; i--){
System.out.print(i+": ");
for(int j = 0; j < SIZE; j++){
..................................................
.................这部份怎完成.....................
..................................................
//印X
if(num[i][j] == -1){
System.out.print("X ");
continue;
}else{
System.out.print(num[i][j]+" ");
}
}
System.out.println();
}
System.out.print(" ");
for(int k = 1; k <= 11; k++){System.out.print("=");}
System.out.print("\n"+" ");
for(int k = 0; k <= 5; k++){System.out.print(k+" ");}
System.out.println("\n");
}
}
注:不使用类方法 ,还不懂
我帮你写了一个,你看看怎么样。效率可能有点低,你自己优化一下吧~
注释写得比较详细了,你应该能看懂。
如果有语法元素不清楚的话就去查查书吧。
总之要明白:Java的数组都是对象,无论是几维的。package com;import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;public class Test2 { enum Direct{UP, DOWN, LEFT, RIGHT};//一个枚举量,用来表示方向
static final int MIN_CELL_SIZE = 3;//常量,表示至少连续多少个
public static void main(String args[]) {
// Define Variable
// Scanner scan = new Scanner(System.in);
//读文件更便于测试,这里暂时改成了读文件。文件里即要找的那个矩阵
Scanner scan = null;
try {
scan = new Scanner(new File("data/matrix.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final int SIZE = 6;
int[][] num = new int[SIZE][SIZE]; // Prompt for input
System.out.println("Enter the initial Grid: ");
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < 6; j++) {
num[i][j] = scan.nextInt();
}
}
// Display Before
System.out.println("\n" + "Before:");
for (int i = num.length - 1; i >= 0; i--) {
System.out.print(i + ": ");
for (int j = 0; j < SIZE; j++) {
System.out.print(num[i][j] + " ");
}
System.out.println();
}
System.out.print(" ");
for (int k = 1; k <= 11; k++) {
System.out.print("=");
}
System.out.print("\n" + " ");
for (int k = 0; k <= 5; k++) {
System.out.print(k + " ");
}
System.out.println("\n"); //添加的函数,用来找出符合条件的单元
ArrayList<MatchingCell> cellList = findMatchingCells(num);
//添加的函数,设置相应的值
setValue(num, cellList);
// Display After
System.out.println("\n" + "After:");
for (int i = num.length - 1; i >= 0; i--) {
System.out.print(i + ": ");
for (int j = 0; j < SIZE; j++) {
// 印X
if (num[i][j] == -1) {
System.out.print("X ");
continue;
} else {
System.out.print(num[i][j] + " ");
} }
System.out.println();
}
System.out.print(" ");
for (int k = 1; k <= 11; k++) {
System.out.print("=");
}
System.out.print("\n" + " ");
for (int k = 0; k <= 5; k++) {
System.out.print(k + " ");
}
System.out.println("\n"); } /**
* 用来找出符合条件的单元
* @param matrix 输入的矩阵
* @return 包含所有符合条件的单元的容器
*/
private static ArrayList<MatchingCell> findMatchingCells(int[][] matrix) {
// TODO Auto-generated method stub
final int lineSize = matrix.length;
final int colSize = matrix[0].length;
ArrayList<MatchingCell> cellList = new ArrayList<MatchingCell>();
//以每一个元素为中心试探。
for ( int i=0; i<lineSize; i++ ){
for ( int j=0; j<colSize; j++ ){
check(matrix, j, i, cellList);
}
}
return cellList;
} /**
* 从上下左右四个方向查找某一个点是否符合要求,是则将其加入容器
* @param matrix 输入的矩阵
* @param i 元素横坐标
* @param j 元素纵坐标
* @param cellList 包含符合条件元素的容器
*/
private static void check(int[][] matrix, final int i, final int j,
ArrayList<MatchingCell> cellList) {
// TODO Auto-generated method stub
int toBeCheck = matrix[i][j];
int index = 0;
int count = 0;
MatchingCell tempCell = null;
//Upwards
index = i-1;
count = 0;
while(index >= 0){
if( matrix[index][j] != toBeCheck )
break;
index--;
}
count = i - index;
if (count >= MIN_CELL_SIZE){
tempCell = new MatchingCell(i, j, count, Direct.UP);
cellList.add(tempCell);
}
//Downwards
index = i+1;
count = 0;
while(index < matrix.length){
if( matrix[index][j] != toBeCheck )
break;
index++;
}
count = index - i;
if (count >= MIN_CELL_SIZE){
tempCell = new MatchingCell(i, j, count, Direct.DOWN);
cellList.add(tempCell);
}
//Left
index = j-1;
count = 0;
while(index >= 0){
if( matrix[i][index] != toBeCheck )
break;
index--;
}
count = j - index;
if (count >= MIN_CELL_SIZE){
tempCell = new MatchingCell(i, j, count, Direct.LEFT);
cellList.add(tempCell);
}
//Right
index = j+1;
count = 0;
while(index < matrix[0].length){
if( matrix[i][index] != toBeCheck )
break;
index++;
}
count = index - j;
if (count >= MIN_CELL_SIZE){
tempCell = new MatchingCell(i, j, count, Direct.RIGHT);
cellList.add(tempCell);
}
}
/**
* 根据cellList的记录把相应的值改为-1
* @param matrix 需要改的矩阵
* @param cellList 存放所有单元的容器
*/
private static void setValue(int[][] matrix, ArrayList<MatchingCell> cellList) {
for ( MatchingCell tempCell: cellList){
switch(tempCell.direct){
case UP:
for ( int i=tempCell.lineIndex; i>tempCell.lineIndex - tempCell.num; i-- ){
matrix[i][tempCell.colIndex] = -1;
}
break;
case DOWN:
for ( int i=tempCell.lineIndex; i<tempCell.lineIndex + tempCell.num; i++ ){
matrix[i][tempCell.colIndex] = -1;
}
break;
case LEFT:
for ( int i=tempCell.colIndex; i>tempCell.colIndex - tempCell.num; i-- ){
matrix[tempCell.lineIndex][i] = -1;
}
break;
case RIGHT:
for ( int i=tempCell.colIndex; i<tempCell.colIndex + tempCell.num; i++ ){
matrix[tempCell.lineIndex][i] = -1;
}
break;
}
}
} /**
* 用来封装每一个查找到的单元。
* @author Michael
*
*/
static class MatchingCell {
final int lineIndex;//横坐标
final int colIndex;//纵坐标
final int num;//元素个数
final Direct direct;//方向
MatchingCell(int i, int j, int n, Direct d){
lineIndex = i;
colIndex = j;
num = n;
direct = d;
}
}
}
那道题其步骤我猜是
1。叫用户输入一大堆数值
2。将输出反转的结果
3。其后,在垂直/水平找相应有3个或以上相同的值,将栭ユ为-1
(4)判断有3个或上个-1,就打印的“X”。
否则打印原本的值还有后面的廷伸题如下
2:2 3 4
1:4 5 1
0:1 2 3
-------
0 1 21请用户输入X1,Y1,X2,Y2。
1 1 0 12就打印结果。2:2 3 4
1:4 2 1
0:1 5 3
------
0 1 23。问题使用者要继续吗(0是1 - NO)? 0
(再给用户输入)1请用户输入X1,Y1,X2,Y2。
1 1 2 2
4,打印Erorr:只要相邻的数才可以交换
(再给用户输入)1请用户输入X1,Y1,X2,Y2。
0 0 1 02就打印结果。
2:2 3 4
1:1 2 1
0:4 5 3
------
0 1 2
这部完成了整合打印“X”道题,应该为宝石方块