gpt4 book ai didi

Java - 将返回 boolean 值和索引的 int 数组

转载 作者:行者123 更新时间:2023-12-01 18:36:19 25 4
gpt4 key购买 nike

java 有没有办法检查指定用户输入的整数数组并返回 boolean 值及其所在的索引?我有我的办法,但我也想知道它在哪个索引中找到了确切的整数

代码片段:

int numUser = Integer.parseInt(inputUser);
boolean ispresent = false;

for(int x = 0; x<=4; x++){
if(sum[x] == numUser){
ispresent = true;
}else{
ispresent = false;
}
}

if(ispresent== true){
System.out.println("The number is in the array");
}else{
System.out.println("The number is not in the array");
}

最佳答案

当在数组中找不到它时,您可能会返回 -1。由于数组中没有-1索引,因此可以判断它没有出现在数组中。

如果该值确实出现在数组中,则可以返回该索引。因为你总是返回一个 int 。

关于Java - 将返回 boolean 值和索引的 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com