☞作业要求
☞解题思路
1.使用for循环输出数列中包含的元素
2.在for循环外定一个值为0的变量,使用for循环,逐个将元素累加到这个变量里面
3.在for循环外面设定一个布尔类型的变量,将变量的值设定为false,使用for循环,将数组里面的数,逐一与输入的数字进行对比,如果有相等的,就将这个布尔变量设定为true,在for循环外面使用if语句判断这个布尔变量的值,如果值为真就标示猜中,如果值不为真就标示没猜中
☞代码内容
import java.util.Scanner;
/**
* @auther::周小龙
* @Description: 猜数字
* @program: Xunhuan
* @create: 2019-07-03 09:46
*/
public class GuessNum {
static Scanner sc= new Scanner(System.in);
public static void main(String[] args) {
int[] nums = new int[]{8,4,2,1,23,44,55};
//定义一个变量,用来储存总分
int sum = 0;
//定义一个布尔变量,用来判断是否猜中
boolean boo = false;
System.out.println("\n猜一下数组里面有哪些元素:");
int guess = sc.nextInt();
for (int num : nums) {
if (num == guess){
boo = true;
}
}
System.out.println("数组元素分别是:");
for (int num : nums) {
sum += num;
System.out.print(num+"\t");
}
System.out.println("\n-------------------------");
if (boo){
System.out.println("\n恭喜你,猜中了,数组中包含 "+guess);
}else {
System.out.println("\n哈哈,你没猜中,数组中不包含 "+guess);
}
System.out.println("--------------------------");
System.out.println("数列中所有值的和为: "+sum);
}
}
☞运行结果
☞错误记录
无
转载于:https://www.cnblogs.com/twuxian/p/11125067.html