春宵一刻值千金,花有清香月有陰。
點贊再看,養成習慣。
問題描述
給出一個包含n個整數的數列,問整數a在數列中的第一次出現是第幾個。
輸入格式
第一行包含一個整數n。
第二行包含n個非負整數,爲給定的數列,數列中的每個數都不大於10000。
第三行包含一個整數a,爲待查找的數。
輸出格式
如果a在數列中出現了,輸出它第一次出現的位置(位置從1開始編號),否則輸出-1。
樣例輸入
6
1 9 4 8 3 9
9
樣例輸出
2
數據規模與約定
1 <= n <= 1000。
import java.util.Scanner;
public class LookupInteger {
public static void main(String[] args) {
//
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int[] arr=new int[a];
for (int i = 0; i < arr.length; i++)
arr[i]=sc.nextInt();
int s=sc.nextInt();
boolean b=true;
for (int i = 0; i < a; i++)
if(arr[i]==s) {
b=false;
System.out.println(i+1);
break;
}
if(b)
System.out.println(-1);
sc.close();
}
}
sc.close():
在使用完scanner類的方法後,提示沒有關閉sc接口。
解決辦法:
在方法的末尾加sc.close();