蓝桥杯- 基础练习:查找整数

蓝桥杯- 基础练习:查找整数_第1张图片

蓝桥杯- 基础练习:查找整数_第2张图片

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int ints[] = new int[size];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = scanner.nextInt();
        }
        int findNum = scanner.nextInt();
        for (int i = 0; i < ints.length; i++) {
            if (ints[i] == findNum) {
                System.out.println(i+1);
                break;
            }else if (i+1==ints.length) {
                System.out.println("-1");    //不要忘了输出 -1   ,我纠结了程序半天才发现,
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/wzqjy/p/8284257.html

你可能感兴趣的:(java)