package tag; import java.util.Scanner; /* * 有一个数列:8,4,2,1,23,344,12 * */ public class Test2 { public static void main(String[] args) { /* * 循环输出数列的值 */ int[] number = { 8, 4, 2, 1, 23, 344, 12 }; for (int i = 0; i < number.length; i++) { System.out.print(number[i]+" "); } System.out.println();System.out.println(); /* * 求数列中所有数值的和 */ int sum = 0; for (int i = 0; i < number.length; i++) { sum += number[i]; } System.out.print("数列的和为:" + sum); System.out.println();System.out.println(); /* * 猜数游戏:从键盘中输入任意一个数据,判断数列是否包含次数 */ System.out.println("请输入要猜的数"); Scanner scanner = new Scanner(System.in); try { int num = scanner.nextInt(); for (int i = 0; i < number.length; i++) { if(num == number[i]) { System.out.println("数列中包含该数字"); return; } } System.out.println("数列中不包含该数字"); } catch (Exception e) { System.out.println("输入不正确"); } } }