当我输入不在列表中的数字时,我试图显示“找不到数字” ... 我无法弄清楚代码出了什么问题。
我的代码:
import java.util.*;
public class Searchu {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int [][] x = new int [1][5];
int k , z=0 , y , item , item1 ;
System.out.println( "Enter 5 numbers: ");
for ( y= 0 ; y < 5 ; y++)
for ( z =0 ; z < 1 ; z++)
x[z][y] = console.nextInt();
System.out.println( "Search Number: ");
k = console.nextInt();
for ( item= 0 ; item < 5 ; item++) {
for ( item1= 0 ; item1 < 1 ; item1++) {
if (x[item1][item] == k) {
System.out.println( " Position: "+ (item+1));
break;
}}}
if (x[z][y] == 5)
System.out.print("Number not found...");
}}
答案 0 :(得分:1)
只需创建一个Arraylist并在其中添加所有元素。对于这种特殊情况,ArrayList不太复杂,更好。每当您需要检查列表中是否存在任何元素时,只需使用contains方法即可。
import java.util.*;
public class Searchu {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
ArrayList<Integer> x = new ArrayList<>();
int k, z = 0, y, item, item1;
System.out.println( "Enter 5 numbers: ");
for ( y= 0 ; y < 5 ; y++)
x.add(console.nextInt());
System.out.println( "Search Number: ");
k = console.nextInt();
if(x.contains(k)){
System.out.println("found");
} else {
System.out.println("not found");
}
}
}
答案 1 :(得分:0)
您的错误是最后一次检查。我添加了一个关于是否找到号码的标志。我同意这些意见,您无需使用2D阵列。
public static void main(String[] args) {
int[][] x = new int[1][5];
int k, z = 0, y, item, item1;
System.out.println("Enter 5 numbers: ");
for (y = 0; y < 5; y++)
for (z = 0; z < 1; z++)
x[z][y] = console.nextInt();
System.out.println("Search Number: ");
k = console.nextInt();
boolean isFound = false;
for (item = 0; item < 5; item++) {
for (item1 = 0; item1 < 1; item1++) {
if (x[item1][item] == k) {
System.out.println(" Position: " + (item + 1));
isFound = true;
break;
}
}
}
if (!isFound)
System.out.print("Number not found...");
}
如果使用一维数组,则代码将如下所示:
public static void main(String[] args) {
int[] array = new int[5];
int target;
System.out.println("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
array[i] = console.nextInt();
}
System.out.println("Search Number: ");
target = console.nextInt();
boolean isFound = false;
for (int i = 0; i < 5; i++) {
if (array[i] == target) {
System.out.println("Position: " + (i + 1));
isFound = true;
break;
}
}
if (!isFound) {
System.out.print("Number not found...");
}
}
答案 2 :(得分:0)
请验证您的itemindex何时达到limit(5)并标记第一个“ for”,以中断所有搜索迭代。
public static void main(String[] args) {
int[][] x = new int[1][5];
int k, z = 0, y, item, item1;
System.out.println("Enter 5 numbers: ");
for (y = 0; y < 5; y++)
for (z = 0; z < 1; z++)
x[z][y] = console.nextInt();
System.out.println("Search Number: ");
k = console.nextInt();
ext:
for (item = 0; item < 5; item++) {
for (item1 = 0; item1 < 1; item1++) {
if (x[item1][item] == k) {
System.out.println(" Position: " + (item + 1));
break ext;
}
}
}
if (item == 5)
System.out.print("Number not found...");
}
答案 3 :(得分:0)
使用“ break”仅从第二个for循环退出,请尝试以下代码。
import java.util.*;
public class Searchu {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int [][] x = new int [1][5];
int k , z=0 , y , item , item1,pos=0 ;
boolean flag=false;
System.out.println( "Enter 5 numbers: ");
for ( y= 0 ; y < 5 ; y++)
for ( z =0 ; z < 1 ; z++)
x[z][y] = console.nextInt();
System.out.println( "Search Number: ");
k = console.nextInt();
for ( item= 0 ; item < 5 ; item++) {
for ( item1= 0 ; item1 < 1 ; item1++) {
if (x[item1][item] == k) {
flag=true;
pos=item+1;
}}}
if(flag)
{
System.out.println( " Position: "+ (pos));
}
else
{
System.out.print("Number not found...");
}
}}