Java基础第四天

1、Scanner类使用(两数之和,三数最大值)

public class Demo01Scanner {
  public static void main(String[] args) {
    //System.out.println(“结果是:” + sum());
    System.out.println(“最大值是” + max());
  }
  //两数之和
  public static int sum() {
    Scanner sc = new Scanner(System.in);//System.in表示从键盘输入
    System.out.println(“请输入第一个数字”);
    int a = sc.nextInt();
    System.out.println(“请输入第二个数字”);
    int b = sc.nextInt();
    return a + b;
  }
  //求最值
  public static int max() {
    Scanner sc = new Scanner(System.in);
    System.out.println(“请输入第一个数字”);
    int a = sc.nextInt();
    System.out.println(“请输入第二个数字”);
    int b = sc.nextInt();
    System.out.println(“请输入第三个数字”);
    int c = sc.nextInt();
    int max = a;
    if (b > max) {
      max = b;
    }
    if (c > max) {
      max = c;
    }
    return max;
  }
}

2、匿名对象作为方法的参数和返回值

使用匿名对象作为方法的参数和返回值,匿名对象只能使用一次。
public class Demo01Anonymous {
  public static void main(String[] args) {
    anonymousParm(new Scanner(System.in));
    Scanner sc = getScanner();
    int num = sc.nextInt();
    System.out.println(“输入的是” + num);
  }
  public static void anonymousParm(Scanner sc) {
    System.out.println(“输入的是” + sc.nextInt());
  }
  public static Scanner getScanner() {
    return new Scanner(System.in);
  }
}

3、Random类使用(生成1-100随机数字,猜数字游戏)

public class Demo01Random {
  public static void main(String[] args) {
    //Random r = new Random();
    //int num = r.nextInt();//可能生成int范围内的任何数字(-21亿到21亿)
    //int num = r.nextInt(100);//左闭右开,生成0-99
    //randomInt();
    guessInt();
  }
  //生成1-n(n可以取到)
  public static void randomInt() {
    int n = new Scanner(System.in).nextInt();
    Random r = new Random();
    for (int i = 0; i < 20; i++) {
    int num = r.nextInt(n) + 1;//本来范围是0到n-1,加1后是1到n
    System.out.println(“随机数是” + num);
    }
  }
  //猜数字游戏
  public static void guessInt() {
    /*方法一
    Random r = new Random();
    int result = r.nextInt(100) + 1;
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    while(num != result) {
      if (num > result) {
        System.out.println(“大了”);
      } else {
        System.out.println(“小了”);
      }
    num = sc.nextInt();
    }
    System.out.println(“猜对了,答案就是” + num);
    */
    //方法二
    Random r = new Random();
    int result = r.nextInt(100) + 1;
    Scanner sc = new Scanner(System.in);
    while(true) {
      int num = sc.nextInt();
      if (num > result) {
        System.out.println(“大了”);
      } else if (num < result) {
        System.out.println(“小了”);
      } else {
        System.out.println(“猜对了,答案就是” + num);
        break;
      }
    }
    System.out.println(“游戏结束”);
    //如果限定次数,可以用for循环
  }
}

4、对象数组的使用

定义Person类
public class Person {
  private String name;
  private int age;
  public Person() {
  }
  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}
public class Demo01ObjectArray {
  public static void main(String[] args) {
    Person[] personArray = new Person[3];
    Person one = new Person(“张三”, 18);
    Person two = new Person(“李四”, 28);
    Person three = new Person(“王五”, 22);
    personArray[0] = one;//将one中存储的对象地址值赋值给对象数组的元素
    personArray[1] = two;
    personArray[2] = three;
    System.out.println(personArray[0]);//输出的是地址值
    System.out.println(personArray[1]);
    System.out.println(personArray[2]);
    System.out.println(personArray[0].getName());
    System.out.println(personArray[0].getAge());
  }
}

5、ArrayList集合的使用

1、ArrayList,对于ArrayList,有一个表示泛型。
2、泛型表示集合中的所有元素都是统一的一种类型。
3、泛型只能是引用类型,不能是基本类型。因为集合存储的是地址值,而基本类型没有地址值。
4、所以要存储基本类型要使用基本类型的包装类。(Byte、Short、Integer、Long、Float、Double、Character、Boolean)

public class Demo01ArrayList {
  public static void main(String[] args) {
    ArrayList list = new ArrayList<>();
    System.out.println(list);//直接打印ArrayList类的对象,输出的是[],不是内存地址值。
    list.add(“张三”);//返回值是布尔类型,表示添加是否成功。但对于Arraylist集合,添加操作是一定会成功的。所以返回值可用可不用。
    list.add(“李四”);
    list.add(“王五”);
    System.out.println(list);
    System.out.println(list.get(1));//索引和数组一样从0开始
    String name = list.remove(1);
    System.out.println(name);
    System.out.println(list);
    int size = list.size();
    System.out.println(size);
    //遍历集合
    for (int i = 0; i < size; i++) {
      System.out.println(list.get(i));
    }
    //存储基本类型数据
    ArrayList listA = new ArrayList<>();
    listA.add(100);
    listA.add(500);
    listA.add(900);
    System.out.println(listA);
    System.out.println(listA.get(1));
    int num = listA.remove(1);
    System.out.println(num);
    System.out.println(listA);
  }
}

6、ArrayList集合的小练习

1、生成6个1-33的随机数字加入集合,并遍历集合。
2、定义4个学生类对象,添加到集合并遍历。
3、用{张三@李四@王五}的格式打印集合,ArrayList集合作为参数,传递进去的是对象的地址值。
4、用大集合存储20个随机数字,然后将偶数放进小集合。ArrayList集合作为返回值,返回的也是对象的地址值。
定义Student类
public class Student {
  private String name;
  private int age;
  public Student() {
  }
  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}
public class Demo02ArrayListPra {
  public static void main(String[] args) {
    //randomSix();
    //studentList();
    //ArrayList list = new ArrayList<>();
    //list.add(“张三”);
    //list.add(“李四”);
    //list.add(“王五”);
    //list.add(“赵六”);
    //printList(list);
    ArrayList listA = new ArrayList<>();
    Random r = new Random();
    for (int i = 0; i < 20; i++) {
      int num = r.nextInt(100) + 1;
      listA.add(num);
    }
   ArrayList list;
    list = chooseList(listA);
    System.out.println(listA);
    System.out.println(list);
  }
  //生成6个1-33的随机数字加入集合,并遍历集合。
  public static void randomSix() {
    ArrayList list = new ArrayList<>();
    Random r = new Random();
    for (int i = 0; i < 6; i++) {
      int num = r.nextInt(33) + 1;
      list.add(num);
    }
    for (int i = 0; i < list.size(); i++) {
      System.out.println(list.get(i));
    }
  }
  //定义4个学生类对象,添加到集合并遍历。
  public static void studentList() {
    ArrayList stus = new ArrayList<>();
    Student stu1 = new Student(“张三”, 18);
    Student stu2 = new Student(“李四”, 19);
    Student stu3 = new Student(“王五”, 20);
    Student stu4 = new Student(“赵六”, 21);
    stus.add(stu1);
    stus.add(stu2);
    stus.add(stu3);
    stus.add(stu4);
    for (int i = 0; i < stus.size(); i++) {
      System.out.println(stus.get(i));//输出的是对象地址值
      System.out.print(stus.get(i).getName());
      System.out.println(stus.get(i).getAge());
    }
  }
  //用{张三@李四@王五}的格式打印集合,ArrayList集合作为参数,传递进去的是对象的地址值。
  public static void printList(ArrayList 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.print("@");
      }
    }
    System.out.println("}");
  }

//用大集合存储20个随机数字,然后将偶数放进小集合。ArrayList集合作为返回值,返回的也是对象的地址值。
  public static ArrayList chooseList(ArrayList listA) {
    ArrayList listB = new ArrayList<>();
    for (int i = 0; i < listA.size(); i++) {
      if (listA.get(i) % 2 == 0) {
        listB.add(listA.get(i));
      }
    }
    return listB;
  }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值