如何确保已填充的 Space 不会在二维数组中再次占用?
How to Make Sure a Space that is filled is not taken up again in a 2D array?
这是我第一次访问这个网站。我对 java 和一般编码非常陌生,所以我很可能需要深入的解释。我正在编写一个程序,需要 12 名学生并将他们安排在 5 行 6 列阵列教室的座位上。我必须防止越界异常并防止学生占用已经填满的座位。 我需要帮助以确保学生不会占用已经被填满的座位。我不知道该怎么做。 到目前为止,这是我的代码:
public class Classroom
{
public static void printArray(char[][] array)
{
for(int row=0;row<5;++row)
{
System.out.print("| ");
for(int col =0;col<6;++col)
System.out.print(array[row][col] + "| ");
System.out.println();
}
for(int col =0;col<6;++col)
System.out.print("---");
System.out.println();
}
public static void main()
{
Scanner reader = new Scanner(System.in);
char[][] array = new char[5][6];
for(int i =0;i<5;++i)
for(int j=0;j<6;++j)
array[i][j] = ' ';
printArray(array);
for(int x =1;x<14;++x)
{
System.out.print("Choose column (1-6) for a student: ");
int column = reader.nextInt();
System.out.print("Choose row (1-5) for a student: ");
int row = reader.nextInt();
if(column <1 || column>6 || row<1 || row>5)
{
System.out.println();
System.out.println("Column should be from 1 to 6. Row should be from 1 to 5.");
System.out.println("The program will restart from student 1.");
System.out.println();
x=1;
for(int i =0;i<5;++i)
for(int j=0;j<6;++j)
array[i][j] = ' ';
continue;
}
for(int i =0;i<5;++i)
for(int j=0;j<6;++j)
array[row-1][column-1] = 'S';
printArray(array);
}
}
}
if (array[row-1][column-1] == 'S') {
System.out.println("This seat is taken! Please choose another.");
continue;
}
当他们试图进入已被占用的座位时,这将打印到控制台。您只需要检查当前 row/column 是否包含一个人,或者在您的情况下,值 'S'
您应该在从扫描器读取该行后立即添加它,以防止它执行您的其余代码。
这是我第一次访问这个网站。我对 java 和一般编码非常陌生,所以我很可能需要深入的解释。我正在编写一个程序,需要 12 名学生并将他们安排在 5 行 6 列阵列教室的座位上。我必须防止越界异常并防止学生占用已经填满的座位。 我需要帮助以确保学生不会占用已经被填满的座位。我不知道该怎么做。 到目前为止,这是我的代码:
public class Classroom
{
public static void printArray(char[][] array)
{
for(int row=0;row<5;++row)
{
System.out.print("| ");
for(int col =0;col<6;++col)
System.out.print(array[row][col] + "| ");
System.out.println();
}
for(int col =0;col<6;++col)
System.out.print("---");
System.out.println();
}
public static void main()
{
Scanner reader = new Scanner(System.in);
char[][] array = new char[5][6];
for(int i =0;i<5;++i)
for(int j=0;j<6;++j)
array[i][j] = ' ';
printArray(array);
for(int x =1;x<14;++x)
{
System.out.print("Choose column (1-6) for a student: ");
int column = reader.nextInt();
System.out.print("Choose row (1-5) for a student: ");
int row = reader.nextInt();
if(column <1 || column>6 || row<1 || row>5)
{
System.out.println();
System.out.println("Column should be from 1 to 6. Row should be from 1 to 5.");
System.out.println("The program will restart from student 1.");
System.out.println();
x=1;
for(int i =0;i<5;++i)
for(int j=0;j<6;++j)
array[i][j] = ' ';
continue;
}
for(int i =0;i<5;++i)
for(int j=0;j<6;++j)
array[row-1][column-1] = 'S';
printArray(array);
}
}
}
if (array[row-1][column-1] == 'S') {
System.out.println("This seat is taken! Please choose another.");
continue;
}
当他们试图进入已被占用的座位时,这将打印到控制台。您只需要检查当前 row/column 是否包含一个人,或者在您的情况下,值 'S'
您应该在从扫描器读取该行后立即添加它,以防止它执行您的其余代码。