尝试读取一个值的二维数组并显示其位置。遇到问题在找不到消息时发送消息
Trying to read a 2D Array for a Value and Display its Location. Encountering Issue Delivering a message when it isn't found
我正在读取一个二维数组,当它显示要查找的目标整数的每个实例时,我似乎无法找到嵌套 for 循环显示消息 Number not found
或其他内容的方法像这样。
二维数组有 8 行和 9 列。
程序如下:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == intRequest){
System.out.println("Your number is in Row " + (i) + " and Column " + (j) + ".");
arr[i][j] = 0;
i = 10;
break;
}
}
}
当它找不到数字时,它什么都不打印,这比错误要好,但我似乎想不出一种方法来显示程序的混乱。
我已经尝试计算该数字出现的次数,直到还剩 none 时才显示该消息,当它等于该数字出现的总次数时。这个 stil 不能奇怪地工作。
您可以尝试创建一个标志,并在找到您的号码后将其设置为 true。如果在循环结束时它仍然为假,那么您还没有找到该数字。我还没有测试代码,但它应该看起来像这样。
boolean found = false;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == intRequest) {
System.out.println("Your number is in Row " + (i) + " and Column " + (j) + ".");
arr[i][j] = 0;
found = true;
break;
}
}
}
if (!found) {
System.out.println("Your number wasn't found");
}
您可以创建一个方法来搜索号码,如果找到,您可以 return 自定义对象 class 包含行和列的索引,如果找不到,您可以 return空。
因此,如果此方法 return 为空,您可以知道未找到数字,您可以轻松打印结果。
创建方法和 class 到 return 索引比在方法中打印结果更干净,因为明天您可以在多个地方重用该方法
public void printSearchResult() {
int[][] input2DArray = new int[0][0]; // You need to create input data here
int numberToFind = 0; // pass your number to search
Index index = findIndexOfGivenNumberIn2DArray(input2DArray, numberToFind);
if (index == null) {
System.out.println("Your number wasn't found");
} else {
input2DArray[index.getRow()][index.getColumn()] = 0;
System.out.println("Your number is in Row " + index.getRow() + " and Column " + index.getColumn() + ".");
}
}
private Index findIndexOfGivenNumberIn2DArray(int arr[][], int numberToFind) {
Index index = null;
for (int row = 0; row < arr.length; row++) {
for (int column = 0; column < arr[row].length; column++) {
if (arr[row][column] == numberToFind) {
index = new Index(row, column);
return index;
}
}
}
return index;
}
private static class Index {
private int row;
private int column;
public Index(int row, int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
我正在读取一个二维数组,当它显示要查找的目标整数的每个实例时,我似乎无法找到嵌套 for 循环显示消息 Number not found
或其他内容的方法像这样。
二维数组有 8 行和 9 列。
程序如下:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == intRequest){
System.out.println("Your number is in Row " + (i) + " and Column " + (j) + ".");
arr[i][j] = 0;
i = 10;
break;
}
}
}
当它找不到数字时,它什么都不打印,这比错误要好,但我似乎想不出一种方法来显示程序的混乱。
我已经尝试计算该数字出现的次数,直到还剩 none 时才显示该消息,当它等于该数字出现的总次数时。这个 stil 不能奇怪地工作。
您可以尝试创建一个标志,并在找到您的号码后将其设置为 true。如果在循环结束时它仍然为假,那么您还没有找到该数字。我还没有测试代码,但它应该看起来像这样。
boolean found = false;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == intRequest) {
System.out.println("Your number is in Row " + (i) + " and Column " + (j) + ".");
arr[i][j] = 0;
found = true;
break;
}
}
}
if (!found) {
System.out.println("Your number wasn't found");
}
您可以创建一个方法来搜索号码,如果找到,您可以 return 自定义对象 class 包含行和列的索引,如果找不到,您可以 return空。
因此,如果此方法 return 为空,您可以知道未找到数字,您可以轻松打印结果。
创建方法和 class 到 return 索引比在方法中打印结果更干净,因为明天您可以在多个地方重用该方法
public void printSearchResult() {
int[][] input2DArray = new int[0][0]; // You need to create input data here
int numberToFind = 0; // pass your number to search
Index index = findIndexOfGivenNumberIn2DArray(input2DArray, numberToFind);
if (index == null) {
System.out.println("Your number wasn't found");
} else {
input2DArray[index.getRow()][index.getColumn()] = 0;
System.out.println("Your number is in Row " + index.getRow() + " and Column " + index.getColumn() + ".");
}
}
private Index findIndexOfGivenNumberIn2DArray(int arr[][], int numberToFind) {
Index index = null;
for (int row = 0; row < arr.length; row++) {
for (int column = 0; column < arr[row].length; column++) {
if (arr[row][column] == numberToFind) {
index = new Index(row, column);
return index;
}
}
}
return index;
}
private static class Index {
private int row;
private int column;
public Index(int row, int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}