Scanner sc = new Scanner(System.in); System.in表示从键盘录入数据
public static void main(String[] args) {
//创建随机数对象
Random rd = new Random();
//获取随机数0--100之内的,因为Random类生成的随机数是[0,N),左闭右开区间,所以需要+1
int i = rd.nextInt(100) + 1;
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
while (true){
//键盘录入数据
int a = sc.nextInt();
if(a > i){
System.out.println("猜大了");
}else if (a < i){
System.out.println("猜小了");
}else {
System.out.println("猜对了");
break;
}
}
}
public static void main(String[] args) {
//创建学生数组
Student[] students = new Student[3];
//创建学生对象
Student s1 = new Student("曹操",40);
Student s2 = new Student("刘备",35);
Student s3 = new Student("孙权",30);
//把学生对象作为元素赋值给学生数组
students[0] = s1;
students[1] = s2;
students[2] = s3;
//遍历学生数组
for(int x=0; x<students.length; x++) {
Student s = students[x];
System.out.println(s.getName()+"‐‐‐"+s.getAge());
}
}
public static void main(String[] args) {
//创建String集合
ArrayList<String> list = new ArrayList<>();
//添加元素
list.add("JAVA");
list.add("PHP");
list.add("C++");
//遍历集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public static void main(String[] args) {
//创建学生对象
Student student1 = new Student("赵丽颖",28);
Student student2 = new Student("刘亦菲",29);
Student student3 = new Student("秦岚",32);
//创建学生信息集合
ArrayList<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
//循环遍历学生集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName()+","+list.get(i).getAge());
}
}
public static void main(String[] args) {
//创建存储随机数的集合
ArrayList<Integer> list = new ArrayList<>();
//创建随机数对象
Random rd = new Random();
//获取1--100之间的20个随机数
for (int i = 0; i < 20; i++) {
int random = rd.nextInt(100) + 1;
list.add(random);
}
//20个随机数
System.out.println("20个随机数:"+list);
//调用arrayList方法
arrayList(list);
}
public static ArrayList<Integer> arrayList(ArrayList<Integer> list){
//创建集合用来存储符合要求的偶数
ArrayList<Integer> list1 = new ArrayList<>();
//遍历1--100之间随机数集合,判断其中是偶数的添加到偶数集合list1中
for (int i = 0; i < list.size(); i++) {
//判断list集合中的数哪些是偶数
if (list.get(i) % 2 == 0){
list1.add(list.get(i));
}
}
System.out.println("是偶数的有"+list1.size()+"个:"+list1);
return list1;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |