本题要求从输入的N个整数中查找给定的X。如果找到,输出X的位置(从0开始数);如果没有找到,输出“Not Found”。
输入格式:
输入在第一行中给出两个正整数N(≤20)和X,第二行给出N个整数。数字均不超过长整型,其间以空格分隔。
输出格式:
在一行中输出X的位置,或者“Not Found”。
输入样例1:
5 7
3 5 7 1 9
输出样例1:
2
输入样例2:
5 7
3 5 8 1 9
输出样例2:
Not Found
代码部分:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (// TODO Auto-generated method stub
Scanner in = new Scanner(System.in)) {
int n,x,i;
int flag=0;
n = in.nextInt();
x = in.nextInt();
int[] a = new int[20];
for(i=0;i<n;i++)
{
a[i] = in.nextInt();
}
for(i=0;i<n;i++)
{
if(a[i] == x)
{
System.out.println(i);
flag = 1;
}
}
if(flag==0)
System.out.println("Not Found");
in.close();
}
}
}
java刚入门小白,如有错误或优化欢迎大佬们的指点!