java练习

练习1: 录入学生信息进集合里,录入后遍历

package exerTest.exerTest1;

import java.util.Objects;

public class student {
    String name;
    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;
    }

    @Override
    public String toString() {
        return "exerTest.exerTest1.student{" +
                "name='" + name + '\'' +
                ", age=" + age +"\n"+
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        student student = (student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

//    @Override
//    public int hashCode() {
//        return Objects.hash(name, age);
//    }

}
package exerTest.exerTest1;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        List list = new ArrayList();//创建list容器

        System.out.println("请录入学生信息");

        while(true){
            System.out.println("1:继续录入,0:结束录入");
            int selection = scan.nextInt();//判断是否结束录入
            
            if (selection == 0){
                break;  //结束循环
            }
            
            System.out.println("请输入学生的姓名");
            String name = scan.next();//获取键入的字符串
            
            System.out.println("请输入学生的年龄:");
            String ageS = String.valueOf(scan.nextInt());//获取年龄的字符串

            int age = Integer.parseInt(ageS);//将年龄字符串String转换成int类型
            student s =  new student(name,age);

            list.add(s);//将心创建的对象传入集合

        }

        //遍历
//        for (Object obj:list){
            System.out.println(list.toString());
//        }
        
    }
}

练习二:随机生成字符串,存入集合,并遍历;

package exerTest2;

import java.util.ArrayList;
import java.util.Collection;

public class ListTest1 {
    public static void main(String[] args) {

        //随机生成30个字符,存放在ArrayList
        ArrayList list = new ArrayList();

        for (int i = 0; i <30 ;i++){
            //'a' - 'z'[97 122]

            list.add((char)(Math.random() * (122 - 97 + 1) + 97)+" ");

        }
        System.out.println(list);

        int cCount  = listTest(list , "a");
        int cCount1 = listTest(list , "b");
        int cCount2 = listTest(list , "j");
        int cCount3 = listTest(list , "c");
                System.out.println("a" + cCount);
                System.out.println("b" + cCount1);
                System.out.println("j" + cCount2);
                System.out.println("c" + cCount3);

    }
    //统计
    public static int listTest(Collection list, String s){
        int count = 0;
        for(Object obj:list){
            if(s.equals(obj)){
                count++;
            }
            System.out.println("------------"+ s.equals(obj) +count);
        }
        return count;
    }
}

你可能感兴趣的:(java,开发语言)