Java 数组元素搜索
我是java新手,一直在尝试编写如何搜索多维数组。我的代码适用于找到的元素,但当我输入一个不匹配的元素时,它不会打印出任何内容。请告诉我我的代码有什么问题Java 数组元素搜索,java,arrays,Java,Arrays,我是java新手,一直在尝试编写如何搜索多维数组。我的代码适用于找到的元素,但当我输入一个不匹配的元素时,它不会打印出任何内容。请告诉我我的代码有什么问题 import java.util.Scanner; public class ArraySearch { public static void main (String[] args){ Scanner input = new Scanner(System.in); //lets create the array i
import java.util.Scanner;
public class ArraySearch {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
//lets create the array
int [] [] arrayOfInts = {{1, 2,3,4}, {5,6,7,8},{9,10,11,12}};
//create search variables
System.out.println("Enter the key number to search for in the array: ");
int key = input.nextInt();
boolean foundIt;
//perform search using a for loop
for (int i = 0; i <arrayOfInts.length; i++){
for (int j = 0; j <arrayOfInts[i].length; j++){
if (arrayOfInts[i][j] == key) {
foundIt = true;
if (foundIt) {
System.out.println("found " + key + " at row " +i+ " column " +j);
} else {
System.out.println(key + "is not in the array");
}
}
}
}
}
}
您应该将布尔值初始化为false,因为在使用局部变量之前必须初始化:
boolean foundIt = false;
否则,如果找不到密钥,则当您在if条件下访问它时,它将被取消初始化
未初始化foundIt应该会给您一个compliation错误局部变量foundIt可能尚未初始化,但您有另一个隐藏此错误的错误。打印输出的if语句应该在for循环之外。现在它位于查找匹配项的条件内,因此只有在找到匹配项时才会对其进行计算。您的括号错误。if-else语句
if (foundIt) {
System.out.println("found " + key + " at row " +i+ " column " +j);
} else
{System.out.println(key + "is not in the array");
}
在for循环的检查内
if (arrayOfInts[i][j] == key) {
您可能希望它位于for循环中,以显示每个匹配的消息。但是你应该在for循环的if语句中放一条println消息
if (arrayOfInts[i][j] == key) {
System.out.println("found " + key + " at row " +i+ " column " +j);
并在找不到密钥时打印另一条消息,但这必须在最后完成。确保在开始时初始化布尔值
boolean foundIt = false;
...
//at the end
if(!foundIt) {
System.out.println("found " + key + " at row " +i+ " column " +j);
}
您可以将代码更改为以下内容。代码中有很多问题。必须对{}进行正确的排序,如果这样做,则需要初始化它
我刚刚添加了一个查找所需元素的返回,并将else分支剪切/粘贴到循环的末尾。您的System.out仅在这个if块中
if (arrayOfInts[i][j] == key)
所以如果你没有找到什么东西,就没有打印出来
我会这样做:
...
for (int i = 0; i <arrayOfInts.length; i++){
for (int j = 0; j <arrayOfInts[i].length; j++){
if (arrayOfInts[i][j] == key) {
foundIt = true;
// Tell where you found it
System.out.println("found " + key + " at row " +i+ " column " +j);
}
}
}
// After all check whether you found something anytime
if(!foundIt){
System.out.println(key + "is not in the array");
}
...
这是因为你的打印语句如果找到它。。。else块位于if arrayFints[i][j]==键块内。这意味着如果找不到int,代码将永远不会进入打印位置的内部if检查。 您可以将未找到的移动到末尾。 例如:
boolean foundIt = false;
// perform search using a for loop
for (int i = 0; i < arrayOfInts.length; i++)
{
for (int j = 0; j < arrayOfInts[i].length; j++)
{
if (arrayOfInts[i][j] == key)
{
foundIt = true;
System.out.println("found " + key + " at row " + i + " column " + j);
}
}
}
if (!foundIt)
{
System.out.println(key + "is not in the array");
}
别忘了先将foundIt初始化为false。我建议的解决方案是:
这项工作:
import java.util.Scanner;
public class ArraySearch {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
//lets create the array
int [] [] arrayOfInts = {{1, 2,3,4}, {5,6,7,8},{9,10,11,12}};
//create search variables
System.out.println("Enter the key number to search for in the array: ");
int key = input.nextInt();
//perform search using a for loop
for (int i = 0; i <arrayOfInts.length; i++){
for (int j = 0; j <arrayOfInts[i].length; j++){
if (arrayOfInts[i][j] == key) {
System.out.println("found " + key + " at row " +i+ " column " +j);
return;
}
}
}
System.out.println(key + " is not in the array");
}
}
原因:
operatorif ArrayFints[i][j]==key{}中的命令仅在数组中找到元素时执行,因此无需使用布尔值foundIt;。使用return结束类的执行,因为我们已经找到了想要的。行System.out.printlnkey+不在数组中;应该在两个循环之后,因此只有在我们之前检查过二维数组的每个元素时,它才会起作用。Java中布尔值的默认值为false。@brimborium它仍然需要初始化才能编译代码。@brimborium这仅对类成员有效。局部变量必须初始化。@Paul这是问题的一部分,如果不解决这个问题,就无法解决问题。我编辑了我的答案以使其完整。@АаСаааааааааааааа。它是未初始化的,但不是空的。另外..您可以在找到元素时中断循环,无需遍历所有元素。请尝试使用正确的代码样式格式。我喜欢,但你也可以在谷歌上找到其他风格。通过使用适当的样式,您可以很容易地检测代码中的大量错误。它还增加了一般的可读性。bool的默认值不为null。Src:execute命令Java InitialValues..对于bool,它不是falsenull@Paul只有当ArrayFints[i][j]==键的计算结果为true时,它才会打印某些内容,因为所有的打印语句都在if块内。@Paul否它只在for的if块内打印。因此,仅当在if语句中找到匹配项时,其中foundIt设置为true。内部if子句是不必要的,因为您将foundIt设置为true。为什么在数组中找到元素时我们不能中断?@SaiAvinash是的,我们可以中断。但是如果有重复的元素会发生什么呢?还需要找到它们吗?我不会打断,它显示行和列,这使得每个输出都是唯一的。如果你只想知道它是否被发现,那么你可以打破它以更快。但是这个实现更加完整,因为它显示了所有匹配项。
boolean foundIt = false;
// perform search using a for loop
for (int i = 0; i < arrayOfInts.length; i++)
{
for (int j = 0; j < arrayOfInts[i].length; j++)
{
if (arrayOfInts[i][j] == key)
{
foundIt = true;
System.out.println("found " + key + " at row " + i + " column " + j);
}
}
}
if (!foundIt)
{
System.out.println(key + "is not in the array");
}
bool foundit=false;
for (int i = 0; i <arrayOfInts.length; i++){
for (int j = 0; j <arrayOfInts[i].length; j++){
if (arrayOfInts[i][j] == key)
{
foundIt = true;
break;
System.out.println("found " + key + " at row " +i+ " column " +j);
}
}
}
if(!foundit)
{
system.out.println("Key not found in the array.")
}
import java.util.Scanner;
public class ArraySearch {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
//lets create the array
int [] [] arrayOfInts = {{1, 2,3,4}, {5,6,7,8},{9,10,11,12}};
//create search variables
System.out.println("Enter the key number to search for in the array: ");
int key = input.nextInt();
//perform search using a for loop
for (int i = 0; i <arrayOfInts.length; i++){
for (int j = 0; j <arrayOfInts[i].length; j++){
if (arrayOfInts[i][j] == key) {
System.out.println("found " + key + " at row " +i+ " column " +j);
return;
}
}
}
System.out.println(key + " is not in the array");
}
}