2014-10-06 124 views
-3

我是java新手,一直在试图编写如何搜索多维数组。我的代码适用于找到的元素,但是当我输入不匹配的元素时,它不打印任何内容。请告诉我我的代码有什么问题。数组元素搜索

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"); 
       } 
      } 
     } 
    } 
} 
} 
+1

@АлександрГончаренко'boolean'不能为'null'。它没有初始化,但不是'null'。 – khelwood 2014-10-06 09:31:04

+0

另外..你可以打破循环,当你找到元素,无需遍历所有 – 2014-10-06 09:33:07

+0

请尝试使用正确的代码风格(格式)。我喜欢[Google Java Style](https://google-styleguide.googlecode.com/svn/trunk/javaguide.html),但您也可以在Google上找到其他样式。通过使用适当的样式,您可以轻松检测代码中的很多错误。它也增加了一般的可读性。 – brimborium 2014-10-06 09:33:16

回答

2

你应该初始化布尔为假,因为局部变量必须在使用前进行初始化:

boolean foundIt = false; 

否则,如果没有找到钥匙,foundIt会当你访问它是未初始化在你的条件。

不初始化foundIt应该给你一个compliation错误(The local variable foundIt may not have been initialized),但是你有另一个隐藏这个错误的错误。您打印输出的if语句应该在for循环之外。现在它处于找到匹配的条件之内,所以只有在找到匹配的情况下才会对其进行评估。

+2

Java中'boolean'的默认值是'false'。 – brimborium 2014-10-06 09:26:59

+0

@brimborium它仍然需要初始化以编译代码。 – khelwood 2014-10-06 09:27:30

+0

@brimborium这只适用于班级成员。局部变量必须被初始化。 – Eran 2014-10-06 09:28:01

1

您的包围是错误的。 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循环中以显示每个匹配的消息。但你应该只是把一个println消息if语句内的for循环

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); 
} 
+1

@Paul不,它只打印在for的if内。所以只有当在if语句中找到匹配时,foundIt被设置为true。 – Juru 2014-10-06 09:33:50

+1

啊是的。通过代码审查驱动器出错了。对不起 – 2014-10-06 09:38:16

1

您可以将您的代码更改为以下内容。你的代码有很多问题。你必须做出的{}正确的顺序,如果你这样做,你需要初始化foundIt

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 = false; 
    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); 
      // if found it will change the foundIt to true 
      foundIt = true; 
      } 
     } 
    } 
    if (!foundIt) { 
     System.out.println(key + "is not in the array"); 
    } 
+0

为什么我们不能在数组中找到元素时中断? – 2014-10-06 09:40:42

+1

@SaiAvinash是的,我们可以打破。但是如果有重复的元素会发生什么?还需要找到它们? – 2014-10-06 09:41:21

+0

我不会中断,它显示行和列,并使每个输出都是唯一的。如果你只是想知道它是否被发现,那么你可以打破更快。但是这个实现更完整,因为它显示了所有匹配。 – Juru 2014-10-06 09:43:55

0
// 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"); 

我只是说上查找所需元素的回报,剪切/ else分支粘贴到的结束循环。

+0

内部if子句不是必需的,因为您将foundIt设置为true – Marco 2014-10-06 09:35:55

0

你的System.out只是这里面如果块

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"); 
} 
... 
0

这是因为您的打印语句if(foundIt)... else块位于if(arrayOfInts [i] [j] == key)块内。这意味着如果找不到int,那么如果检查打印的位置,代码就不会进入内部。 您可以将“找不到”移动到最后。 例如:

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。

0

我建议的解决方案是:

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.") 
} 
0

这工作:

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"); 
} 
} 

为什么: 命令在操作if (arrayOfInts[i][j] == key) {}只有当元素在阵列中运行,所以它是没有必要使用您的boolean foundIt;。使用return结束执行类,因为我们已经找到了我们想要的东西。行System.out.println(key + " is not in the array");应该在两个周期之后,所以它只有在我们检查过二维数组的每个元素之后才能工作。