问题描述
基本上,我必须实现多种方法来处理类的二维字符数组(显示、重新着色和缩放)。除了一个问题之外,我已经完成了所有工作 - “缩放”功能旨在显示所选字符及其周围的所有字符,但在没有任何内容可显示的地方显示空白区域。但是我不知道如何让它做到这一点(目前,如果我尝试“放大”到数组边缘周围的任何字符,它只会显示一条错误消息。
下面的代码,我去掉了所有不相关的东西,只留下了显示 (d) 和缩放 (z) 功能。需要明确的是,问题不在于输入超出范围的初始坐标,实际上还有另一种方法可以处理我删除的问题,问题在于“放大”到边缘周围的空间数组,当它尝试显示越界空间时将返回错误消息。
代码:
import java.util.Scanner;
public class CharMapFORHELP {
public static void zoom(char[][] map,int row,int col) {
//the problem method
System.out.print(map[row - 1][col - 1]);
System.out.print(map[row - 1][col]);
System.out.print(map[row - 1][col + 1]);
System.out.println();
System.out.print(map[row][col - 1]);
System.out.print(map[row][col]);
System.out.print(map[row][col + 1]);
System.out.println();
System.out.print(map[row + 1][col - 1]);
System.out.print(map[row + 1][col]);
System.out.print(map[row + 1][col + 1]);
System.out.println(); //adds the newline
}
/**
* displays the given character-based map
* with a leading and trailing blank line.
*/
public static void displayMap(char[][] map) {
for (int row = 0; row < map.length; row++) {
for (int col = 0; col < map[row].length; coL++) {
System.out.print(map[row][col] + " ");
}
System.out.println(); //adds the newline
}
}
// code to convert String array into map. Truthfully
// I don't 100% understand whats going on here,this
// was provided to us already written.
public static char[][] stringToArray(String str) {
char[][] result;
String[] rows;
rows = str.split("\n");
result = new char[rows.length][];
for (int r = 0; r < rows.length; r++) {
result[r] = rows[r].tochararray();
}
return result;
}
public static void main(String[] args) {
//Commands
final char CMD_disPLAY = 'd',CMD_ZOOM = 'z',CMD_QUIT = 'q';
Scanner sc = new Scanner(system.in);
char[][] map; // the character-based map
char command; // user's entered command
int row; // |
int col; // |- command parameters
char fill; // |
String strMap = "####################\n" +
"#### #####\n" +
"# ##\n" +
"## ### ##\n" +
"###### #######\n" +
"## ##\n" +
"#### ## ## ##\n" +
"###### ######### ##\n" +
"####################";
map = stringToArray(strMap);
System.out.println("Char Map");
System.out.println("========");
System.out.println();
System.out.println("Enter commands (? for help)." +
" There are no further prompts after this point.");
do {
//read the next single-character command
command = sc.next().charat(0);
switch (command) {
case CMD_disPLAY:
displayMap(map);
break;
case CMD_ZOOM:
row = sc.nextInt();
col = sc.nextInt();
zoom(map,row,col);
break;
case CMD_QUIT:
break;
}
} while (command != CMD_QUIT);
}
}
解决方法
一种方法是检查数组索引是否越界,然后打印不同的字符。
Wa 可以通过创建一个新方法来检查字符是否越界:
public static String getValue(char[][] map,int row,int col){
//Check the row
if (row >= 0 && row < map.length){
//Check the col
if(col >= 0 && col < map[row].length){
//Return the character only if the row and col are valid
return map[row][col];
}
}
//If the above checks fail then the index is out of bounds.
//So return a placeholder for printing,in this example we use a dash
return "-";
}
现在我们可以修改缩放方法以使用 getValue(map,row,col)
方法,如下所示:
public static void zoom(char[][] map,int col) {
System.out.print(getValue(map,row - 1,col - 1));
System.out.print(getValue(map,col));
System.out.print(getValue(map,col + 1));
System.out.println();
System.out.print(getValue(map,row + 1,col + 1));
System.out.println(); //adds the newline
}