15、从键盘输入一个整数,然后判断数组中是否含有该数,如果存在,输出“猜对了!”,否则输出“Sorry!”

版权声明:制作人:流川枫 https://blog.csdn.net/qq_44739706/article/details/89880608
import java.util.Scanner;

public class Zuoye1 {
	/**
	 * 
	 从键盘输入一个整数, 然后判断数组中是否含有该数, 如果存在,输出“猜对了!”, 否则输出“Sorry!”
	 * 
	 * @param args
	 */

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
		System.out.println("请输入一个整数:");
		int num = input.nextInt();
		boolean choice = false;
		for (int i = 0; i < nums.length; i++) {
			if (nums[i] == num) {
				System.out.println("猜对了");
				choice = true;
				break;
			}
		}
		// 不能再循环中,否则和循环寻找元素有冲突
		if (choice == false) {
			System.out.println("Sorry!");
		}

	}
}

————————————————————————
制作人:流川枫

猜你喜欢

转载自blog.csdn.net/qq_44739706/article/details/89880608