检查数组中是否存在随机数

时间:2020-04-05 01:33:27

标签: java arrays linear-search

如何使用if语句检查数组中是否存在数字?我正在尝试打印“找到”(如果存在),否则打印“未找到”。这是我的代码:

 import jmespath
 #create a compiled expression
 #of the data path
 #similar to re.compile
 #it looks for the quote key and returns its contents
 expression = jmespath.compile('*.quote')
 A = expression.search(data)

#read into a dataframe
 pd.DataFrame(A)

    symbol  companyName primaryExchange calculationPrice
0   AAPL    Apple, Inc.       SQDNAA      close
1   MSFT    Microsoft Corp.    QSAAND     close

2 个答案:

答案 0 :(得分:0)

使用数组值的IntStream并检查它们是否与扫描仪提供的值匹配。

arr5[i] = (int)(Math.random()*100000 + 0);


Scanner input = new Scanner(System.in);
    here i will input my search random number
System.out.print("Input search key: ");
int searchKey = input.nextInt();

if (IntStream.of(arr5).anyMatch(val -> val == searchKey)) {
   // found
} 

答案 1 :(得分:0)

对于每个循环,您都可以按a进行操作。

for ( int number: arr5 ) {
if ( number == searchKey ) {
// do everything you want
System.out.println("my key is in the array");
break;
}
}