当用户键入一个值时,它会检查它是否存在于数组中。在Java中查找元素数组中的元素
import java.util.Scanner;
public class array1 {
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a value");
int num = scan.nextInt();
int [] arraynumbers = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < arraynumbers.length; i++) {
if (arraynumbers[i] == num){
System.out.println("The value you have entered " + num + ", exists in the array");
}else{
System.out.println("The value you have entered does not exist in the array");
}
}
}
}
所以,当过我输入一个号码以测试它打印:
Enter a value
3
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered 3, exists in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
我不是100%肯定,为什么出现这种情况。
问题
- 是不是因为有什么能够阻止它,当它发现在阵列中的一些整理?
- 有没有办法来防止这种情况?
谢谢
输入一个值您输入的值不数组 值中不存在您输入的数组不存在 您输入的值3存在于数组 –
中请标记问题的答案或更新问题 – nullpointer