java 有一个数列:8,4,2,1,23,344,12,实现猜数游戏:从键盘中任意输入一个数据,判断数列中是否包含此数
时间: 2024-06-14 20:06:06 浏览: 136
以下是一个实现猜数游戏的Java代码示例:
```java
package cn.bdqn.demo01;
import java.util.Scanner;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] numbers = {8, 4, 2, 1, 23, 344, 12};
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个数字:");
int guessNumber = scanner.nextInt();
boolean isFound = false;
for (int number : numbers) {
if (number == guessNumber) {
isFound = true;
break;
}
}
if (isFound) {
System.out.println("数列中包含此数!");
} else {
System.out.println("数列中不包含此数!");
}
}
}
```
阅读全文