这更像是一个语法错误,因为我的大部分代码运行得非常好。问题是如果在数组中找不到值,我希望程序将其打印出来。在没有程序写出的情况下,似乎找不到编写代码部分的地方" Not found"在每一行。下面是我的代码和控制台输出。谢谢大家。
代码:
public static void main(String[ ] args)
{
final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
final int[ ] EMPTY = new int[0];
final int MINIMUM = 0;
final int MAXIMUM = 16;
int target;
System.out.println("Searching for numbers in an array.");
for (target = MINIMUM; target <= MAXIMUM; target++)
{
System.out.print("\nIs " + target + " in the array? ");
{
for (int index = 0; index < DATA.length; index++)
{
if ( DATA[index] == target )
System.out.printf("Yes! %d was found at index [%d]", target, index);
}
}
}
控制台输出:
Searching for numbers in an array...
Is 0 in the array?
Is 1 in the array?
Is 2 in the array? Yes! 2 was found at index [0]
Is 3 in the array?
Is 4 in the array? Yes! 4 was found at index [1]
Is 5 in the array?
Is 6 in the array? Yes! 6 was found at index [2]
Is 7 in the array?
Is 8 in the array? Yes! 8 was found at index [3]
Is 9 in the array?
Is 10 in the array? Yes! 10 was found at index [4]
Is 11 in the array?
Is 12 in the array? Yes! 12 was found at index [5]
Is 13 in the array?
Is 14 in the array? Yes! 14 was found at index [6]
Is 15 in the array?
Is 16 in the array?
答案 0 :(得分:0)
你应该在for循环中使用一个变量来判断是否找到了某些东西,你也应该在找到一些东西后退出以避免不必要的搜索。
public static void main(String[ ] args)
{
final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
final int[ ] EMPTY = new int[0];
final int MINIMUM = 0;
final int MAXIMUM = 16;
boolean foundElement;
int target;
System.out.println("Searching for numbers in an array.");
for (target = MINIMUM; target <= MAXIMUM; target++)
{
System.out.print("\nIs " + target + " in the array? ");
foundElement = false;
for (int index = 0; (index < DATA.length && !foundElement); index++)
{
foundElement = (DATA[index] == target);
if ( foundElement )
System.out.printf("Yes! %d was found at index [%d]", target, index);
}
if (!foundElement)
System.out.printf(" Not found");
}
}
另外,您可以阅读本文以了解实现数组项搜索的其他方法:4 ways to search object in array
答案 1 :(得分:0)
快速修复如下:
添加布尔变量
如果找到元素,则将布尔变量设置为true
如果找不到元素,则写入未找到(即布尔变量为假)
PS:不要忘记在每个循环之前将布尔值设为假
答案 2 :(得分:0)
在boolean found = false;
for (int index = 0; index < DATA.length; index++) {
if ( DATA[index] == target ) {
System.out.printf("Yes! %d was found at index [%d]", target, index);
found = true;
break;
}
}
if (!found) {
System.out.printf("No! %d was not found", target);
}
:
rate
答案 3 :(得分:0)
有很多方法可以做到这一点,一切运作良好......
以下是您可以做的一个示例:
final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
final int[ ] EMPTY = new int[0];
final int MINIMUM = 0;
final int MAXIMUM = 16;
int foundIndex;
int target;
System.out.println("Searching for numbers in an array.");
for (target = MINIMUM; target <= MAXIMUM; target++)
{
foundIndex = -1;
System.out.print("\nIs " + target + " in the array? ");
{
for (int index = 0; index < DATA.length; index++)
{
if ( DATA[index] == target ){
foundIndex = index;
break;
}
}
}
if(foundIndex > -1)
System.out.printf("Yes! %d was found at index [%d]", target, foundIndex);
else
System.out.printf("No! %d was not found", target);
}
另一种方法是使用布尔值设置是否找到了:
final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
final int[ ] EMPTY = new int[0];
final int MINIMUM = 0;
final int MAXIMUM = 16;
boolean found;
int target;
System.out.println("Searching for numbers in an array.");
for (target = MINIMUM; target <= MAXIMUM; target++)
{
found = false;
System.out.print("\nIs " + target + " in the array? ");
{
for (int index = 0; index < DATA.length; index++)
{
if ( DATA[index] == target ){
found = true;
System.out.printf("Yes! %d was found at index [%d]", target, index);
break;
}
}
}
if(!found)
System.out.printf("No! %d was not found", target);
}