将 2 个均包含字符的二维数组与 java 组合
Combine 2 2D arrays that both contain chars with java
我必须为我的大学编写代码作为一项任务,我们必须使用 java 重新创建扫雷,并且必须在命令行中运行它。
对于 matchfield,我们必须制作一个最终看起来像下图这样的数组:
Example how it sould look in the end
要选择字段,我们必须使用扫描仪。
例如,如果您想选择字段 C3,则必须在扫描仪 C3 中输入。
目前我在这个领域有点挣扎。
我有 2 个想法,但都没有很好地实现。
在第一次尝试中,我尝试用 2 个 for 循环和 1 个数组创建所有内容,但我的问题是我无法添加 2 个字符,所以我有字符 0 到 9 和字符 A 到 J。
在第二次尝试中,我创建了 3 个数组,一个数组的数字为 0 到 9,另一个数组为 A 到 J,在第三个数组中,我想合并两个数组。现在我想知道这是否可能,如果我可以按照我想要的方式实际组合它们,如果可能的话,有人能给我一些帮助吗?
import java.util.Scanner;
import java.util.Arrays;
public class Minesweeper {
public static void main (String[] args) {
char c = 'A';
char d = '0';
char e = '9';
char f = 'J';
char[][] feldz = new char[11][11];
char[][] feldb = new char[11][11];
char[][] feld = new char[11][11];
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (i == 0 && j == 0) {
feldz[i][j] = ' ';
System.out.print(feldz[i][j] + " |");
}
if (d > e) {
d = '0';
}
if (d <= e && i > 0){
feldz[i][j] = d;
System.out.print(feldz[i][j] + " |");
}
if (i > 0 && j == 10) {
d++;
}
}
System.out.println("");
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (i == 0 && j == 0) {
feldb[i][j] = ' ';
System.out.print(feldb[i][j] + " |");
}
if (i > 0 && j == 0){
feldb[i][j] = ' ';
System.out.print(feldb[i][j] + " |");
}
if (c > f) {
c = 'A';
}
if(c <= f && j > 0){
feldb[i][j] = c;
System.out.print(feldb[i][j] + " |");
c++;
}
if (j == 10){
System.out.println("");
}
}
}
}
}
你实际上不需要 array 来 打印迷宫 , nested loops
就足够了。 2d Array
只需要 store the input
。 请尝试以下代码:
int size = 10;
int [][] maze = new int[size][size];
while (true){
System.out.print(' ');
for (int i = 0; i < size; i++) {
System.out.print('|');
System.out.print((char) ('A' + i));
}
for (int i = 0; i < size; i++) {
System.out.println("");
System.out.print(i);
for (int j = 0; j < size; j++) {
System.out.print('|');
if(maze[i][j] > 0) {
System.out.print(maze[i][j]);
} else {
System.out.print(' ');
}
}
}
int row = -1;
int col = -1;
System.out.println("\nEnter CoOrdinates");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if(input.length() == 2) {
char charAt = input.charAt(0);
if(charAt >= 'A' && charAt <= 'A'+size-1) {
col = charAt-'A';
}
charAt = input.charAt(1);
if(charAt >= '0' && charAt <= '0'+size-1) {
row = charAt-'0';
}
if(row != -1 && col != -1) {
System.out.println("Enter Action");
input = scanner.nextLine();
int action = Integer.parseInt(input);
maze[row][col] = action;
} else {
System.out.println("Incorrect Input");
}
}
}
我必须为我的大学编写代码作为一项任务,我们必须使用 java 重新创建扫雷,并且必须在命令行中运行它。 对于 matchfield,我们必须制作一个最终看起来像下图这样的数组:
Example how it sould look in the end
要选择字段,我们必须使用扫描仪。 例如,如果您想选择字段 C3,则必须在扫描仪 C3 中输入。 目前我在这个领域有点挣扎。
我有 2 个想法,但都没有很好地实现。 在第一次尝试中,我尝试用 2 个 for 循环和 1 个数组创建所有内容,但我的问题是我无法添加 2 个字符,所以我有字符 0 到 9 和字符 A 到 J。
在第二次尝试中,我创建了 3 个数组,一个数组的数字为 0 到 9,另一个数组为 A 到 J,在第三个数组中,我想合并两个数组。现在我想知道这是否可能,如果我可以按照我想要的方式实际组合它们,如果可能的话,有人能给我一些帮助吗?
import java.util.Scanner;
import java.util.Arrays;
public class Minesweeper {
public static void main (String[] args) {
char c = 'A';
char d = '0';
char e = '9';
char f = 'J';
char[][] feldz = new char[11][11];
char[][] feldb = new char[11][11];
char[][] feld = new char[11][11];
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (i == 0 && j == 0) {
feldz[i][j] = ' ';
System.out.print(feldz[i][j] + " |");
}
if (d > e) {
d = '0';
}
if (d <= e && i > 0){
feldz[i][j] = d;
System.out.print(feldz[i][j] + " |");
}
if (i > 0 && j == 10) {
d++;
}
}
System.out.println("");
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (i == 0 && j == 0) {
feldb[i][j] = ' ';
System.out.print(feldb[i][j] + " |");
}
if (i > 0 && j == 0){
feldb[i][j] = ' ';
System.out.print(feldb[i][j] + " |");
}
if (c > f) {
c = 'A';
}
if(c <= f && j > 0){
feldb[i][j] = c;
System.out.print(feldb[i][j] + " |");
c++;
}
if (j == 10){
System.out.println("");
}
}
}
}
}
你实际上不需要 array 来 打印迷宫 , nested loops
就足够了。 2d Array
只需要 store the input
。 请尝试以下代码:
int size = 10;
int [][] maze = new int[size][size];
while (true){
System.out.print(' ');
for (int i = 0; i < size; i++) {
System.out.print('|');
System.out.print((char) ('A' + i));
}
for (int i = 0; i < size; i++) {
System.out.println("");
System.out.print(i);
for (int j = 0; j < size; j++) {
System.out.print('|');
if(maze[i][j] > 0) {
System.out.print(maze[i][j]);
} else {
System.out.print(' ');
}
}
}
int row = -1;
int col = -1;
System.out.println("\nEnter CoOrdinates");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if(input.length() == 2) {
char charAt = input.charAt(0);
if(charAt >= 'A' && charAt <= 'A'+size-1) {
col = charAt-'A';
}
charAt = input.charAt(1);
if(charAt >= '0' && charAt <= '0'+size-1) {
row = charAt-'0';
}
if(row != -1 && col != -1) {
System.out.println("Enter Action");
input = scanner.nextLine();
int action = Integer.parseInt(input);
maze[row][col] = action;
} else {
System.out.println("Incorrect Input");
}
}
}