美文网首页
Scanner 、Random、ArrayList的基础编程题

Scanner 、Random、ArrayList的基础编程题

作者: 我把心事寄流年 | 来源:发表于2018-11-01 20:31 被阅读10次

    生成6个1~33之间的随机整数,添加到集合,并遍历

    package ArrayList;
    
    import java.util.ArrayList;
    import java.util.Random;
    /*
    * 思路
    * 6个--->6.fori
    * 1~33之间--->xxx.nextInt(33)+1;
    * 随机整数--->Random xxx = new Random();
    * 添加到集合--->ArrayList<Integer> xxx = new ArrayList<>(); list.add();
    * 遍历--->xxx.fori;--->list.get(索引);
    *
    * */
    public class ArrayList_4 {
        public static void main(String[] args) {
            Random r = new Random();
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < 6; i++) {
                int num = r.nextInt(33)+1;
                list.add(num);
            }
            System.out.println(list);
            for (int i = 0; i < list.size(); i++) {
                System.out.print(list.get(i)+" ");
            }
        }
    }
    

    自定义4个学生对象,添加到集合,并遍历

    package ArrayList;
    
    import Class_Objiect.Student;
    import java.util.ArrayList;
    /*
    * 思路
    * 自定义4个学生对象--->Student one = new Student();
    *                   Student two = new Student();
    *                   Student three = new Student();
    *                   Student four = new Student();
    * 添加到集合--->ArrayList<Student> list = new ArrayList<>(); &  list.add(内容);
    * 遍历--->list.fori--->list.get(index);
    * */
    public class ArrayList_5 {
        public static void main(String[] args) {
            ArrayList<Student> list = new ArrayList<Student>();
            Student one = new Student("Andy",18);
            Student two = new Student("Bill",19);
            Student three = new Student("Lily",32);
            Student four = new Student("Jane",12);
            list.add(one);
            list.add(two);
            list.add(three);
            list.add(four);
            for (int i = 0; i < list.size(); i++) {
                System.out.println("姓名:"+list.get(i).getName()+"  "+"年龄:"+list.get(i).getAge()+"岁");
            }
        }
    }
    

    定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起集合,使用@分隔每个元素。格式参照 {元素@元素@元素}。

    package ArrayList;
    
    import java.util.ArrayList;
    
    /*
    * 思路
    * ArrayList<数据类型> xxx = new ArrayLIstanbul<>();
    * */
    public class ArrayList_6 {
        public static void main(String[] args) {
            //创建集合
            ArrayList<Integer> list = new ArrayList<>();
            //添加元素
            list.add(1);
            list.add(3);
            list.add(5);
            list.add(7);
            //调用方法
            print(list);
        }
        public static void print(ArrayList<Integer> list) {
            System.out.print("{");//打印  {
            for (int i = 0; i < list.size(); i++) {
                if (i == list.size() - 1) {
                    System.out.print(list.get(i));//打印数字
                } else {
                    System.out.print(list.get(i) + "@");//打印  "数字@"
                }
            }
            System.out.println("}");//打印 }
        }
    }
    

    定义1~30之间(包括1和30),所有偶数元素集合的方法(ArrayList类型作为返回值)

    package ArrayList;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    /*
    * 思路
    * 1-30之间--->xxx.nextInt(30)+1;
    * 所有偶数元素--->if(i%2==0){偶数}
    * 方法 public static String method(ArrayList<String> list)
    * */
    public class ArrayList_7 {
        public static void main(String[] args) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 1; i <=30; i++) {
                if(i%2==0){
                    list.add(i);
                }
            }
            System.out.println(list);
        } 
    }
    

    游戏开始时,会随机生成一个1-100之间的整数 number 。玩家猜测一个数字guessNumber ,会与 number 作比 较,系统提示大了或者小了,直到玩家猜中,游戏结束

    package Random;
    /*思路
    随机--->Random r = new Random();
    1~100-->int number = r.nextInt(100)+1;
    猜测 --->Scanner sc = new Scanner(System.in);--->int guessNumber = sc.nextInt();
    if(guessNumber>number){大了}
    if(guessNumber<number){小了}
    if(guessNumber==number){猜对了}
    guessNumber应该是一个循环;
    * */
    import java.util.Random;
    import java.util.Scanner;
    
    public class Random_4 {
        public static void main(String[] args) {
            Random r = new Random();
            Scanner sc = new Scanner(System.in);
            int number =r.nextInt(100)+1;
            System.out.println("请输入:");
            for (int i = 0; i <100 ; i++) {
                int guessNumber = sc.nextInt();
              if(guessNumber>number){
                  System.out.println("大了");
              }else if(guessNumber<number){
                  System.out.println("小了");
                }else if(guessNumber==number){
                  System.out.println("猜对了");
                    }
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:Scanner 、Random、ArrayList的基础编程题

          本文链接:https://www.haomeiwen.com/subject/wahfxqtx.html