转 java开发实战经典(第二版)P80 4-5

4.5    给出10个整数(int型),然后任意查询一个数字是否存在在该10个数字内。

package book;
import java.util.Scanner;
public class JiOu {
	public static void main(String[] args) {
		int arr[] = { 13, 45, 6, 60, 54, 76, 7, 95, 33, 66 };
		System.out.println("请输入要查询的数字:");
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int count = 0;
		for (int j = 0; j < arr.length; j++) {
			if (a == arr[j]) {
				count++;
			}
		}
		if (count > 0) {
			System.out.println(a + "在这十个数字中!");
		} else {
			System.out.println(a + "不在这十个数字中!");
		}
	}
}

运行结果为;

请输入要查询的数字:
7
7在这十个数字中!

猜你喜欢

转载自blog.csdn.net/javaxiaobaismc/article/details/81058423
4-5