Java定义一个长度为10的整型数组,从键盘对其初始化,然后再输入一个整数n,查找n找到则输出对应下标,若没有则给出提醒
时间: 2023-06-04 10:03:52 浏览: 113
好的,这是Java的代码:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
arr[i] = scanner.nextInt();
}
int n = scanner.nextInt();
int index = -1;
for (int i = 0; i < 10; i++) {
if (arr[i] == n) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("没有找到" + n);
} else {
System.out.println(n + "的下标是" + index);
}
}
}
```
阅读全文