蓝桥杯--基础练习 查找整数(java)

在这里插入图片描述
注意:位置从 1 开始!!!

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int[] nums = new int[sc.nextInt()];
        for(int i = 0;i < nums.length;i++) {
        	nums[i] = sc.nextInt();
        }
        boolean have = false;
        int target = sc.nextInt();
        for(int i = 0;i < nums.length;i++) {
        	if(nums[i] == target) {
        		have = true;
        		System.out.println(i + 1);
        		break;//后面还可能有重复的值,不break会打印多个值
        	}
        }
        if(!have) System.out.println(-1);
        sc.close();
	}
}

发布了48 篇原创文章 · 获赞 4 · 访问量 2940

猜你喜欢

转载自blog.csdn.net/QinLaoDeMaChu/article/details/104087942