java-Random类与ArrayList类练习

目录

前言

       这些题目是我从网上找来的,但是代码是我自己打出来的,有些代码很多余,或者说不够简洁,我只从实现功能实现了代码,所以有些写得不是很好,以后再修改吧,主要是拿来练手而已。

题目1

  1. 随机验证码。
  • 随机生成十组六位字符组成的验证码。
  • 验证码由大小写字母、数字字符组成。
  • 代码实现,效果如图所示:
    在这里插入图片描述

代码如下:

public class Test01 {
    public static void main(String[] args) {
        char[] string = new char[]{
          'A','B','C','D','E','F','G','H','I','j',
          'K','L','M','N','O','P','Q','R','S','T',
          'U','V','W','X','Y','Z','a','b','c','d',
          'e','f','g','h','i','j','k','l','m','n',
          'o','p','q','r','s','t','u','v','w','x',
          'y','z','1','2', '3','4','5','6','7','8',
          '9'
        };

        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for(int j = 0;j < 10; j++){
            for(int i =0 ;i < 6;i++){
                stringBuilder.append(string[random.nextInt(string.length)]);
            }
            System.out.println("随机验证码:"+stringBuilder);
        }

    }
}

题目2

  1. 键盘录入学生信息,保存到集合中。
  • 循环录入的方式,1:表示继续录入,0:示结束录入。
  • 定义学生类,属性为姓名,年龄,使用学生对象保存录入数据。
  • 使用ArrayList集合,保存学生对象,录入结束后,遍历集合。
    代码实现,效果如图所示:
    在这里插入图片描述

代码:

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 Test02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String choice="";
        ArrayList<Student> arrayList = new ArrayList<Student>();
        while(true){
            System.out.println("1. 录入信息 0. 退出");
            choice = scanner.nextLine();
            switch (choice){
                case "1":
                    inputmessage(arrayList);
                    break;
                case "0":
                    System.out.println("录入完毕");
                    for (Student student:arrayList
                         ) {
                        System.out.println("学生姓名 = "+student.getName()+", 年龄="+student.getAge());
                    }
                    System.exit(0);
            }
        }
    }

    public static void inputmessage(ArrayList arrayList){
        System.out.println("请输入姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入年龄:");
        int age = scanner.nextInt();

        Student student = new Student(name,age);
        arrayList.add(student);

    }
}

题目3

  1. 集合工具类。
  • 定义findIndex方法,在某集合中,查找某元素,返回第一次出现的索引。

  • 定义replace方法,将某集合中的某元素,全部替换为新元素。

  • 代码实现,效果如图所示:
    在这里插入图片描述

代码:

public class Test03 {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(83);
        arrayList.add(72);
        arrayList.add(83);
        arrayList.add(83);
        arrayList.add(77);
        arrayList.add(75);
        arrayList.add(72);
        arrayList.add(72);
        arrayList.add(67);
        arrayList.add(65);

        int index = findIndex(arrayList, 72);
        System.out.println("72的索引是:[");
        System.out.println("替换前:");
        for(int i =0;i<arrayList.size();i++){
            if( i == arrayList.size() - 1){
                System.out.print(arrayList.get(i)+"]");
                break;
            }
            System.out.print(arrayList.get(i)+", ");

        }
        System.out.println("");
        replace(arrayList,27,72);
        System.out.println("替换后:");
        for(int i =0;i<arrayList.size();i++){
            if( i == arrayList.size() - 1){
                System.out.print(arrayList.get(i)+"]");
                break;
            }
            System.out.print(arrayList.get(i)+", ");

        }

    }

    public static int findIndex(ArrayList<Integer> arrayList,int data){
        int index = -1;
        Integer temp = (Integer) data;
        for (Integer i:arrayList
             ) {
            if( i == data){
                index++;
                return index;
            }
            index++;
        }
        return index;
    }


    public static void replace(ArrayList<Integer> arrayList,Integer data,Integer replaceofdata){
        for(int i =0;i < arrayList.size();i++){
            if(arrayList.get(i).equals(replaceofdata)){
                arrayList.add(i,data);
                arrayList.remove(i+1);
            }
        }
    }
}

题目4

  1. 统计数字出现次数。
  • 定义getNumList方法,随机生成100个数字,数字范围从1到10。

  • 定义printCount方法,统计每个数字出现的次数并打印到控制台。

  • 代码实现,部分效果如图所示:
    在这里插入图片描述

代码:

public class Test04 {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = getNumList();
        printCount(arrayList);
    }

    public static ArrayList<Integer> getNumList(){
        ArrayList<Integer> arrayList = new ArrayList<>();
        Random random  = new Random();
        for(int i =0;i < 100;i++){
            arrayList.add(random.nextInt(10)+1);
        }
        return arrayList;

    }

    public static void printCount(ArrayList<Integer> arrayList){
        int[] num = new int[10];

        for(int i =0;i<arrayList.size();i++){
            switch (arrayList.get(i)){
                case 1:
                     num[0]++;
                    break;
                case 2:
                    num[1]++;
                    break;
                case 3:
                    num[2]++;
                    break;
                case 4:
                    num[3]++;
                    break;
                case 5:
                    num[4]++;
                    break;
                case 6:
                    num[5]++;
                    break;
                case 7:
                    num[6]++;
                    break;
                case 8:
                    num[7]++;
                    break;
                case 9:
                    num[8]++;
                    break;
                case 10:
                    num[9]++;
                    break;

            }


        }
        System.out.println("数字:1 -- "+num[0]);
        System.out.println("数字:2 -- "+num[1]);
        System.out.println("数字:3 -- "+num[2]);
        System.out.println("数字:4 -- "+num[3]);
        System.out.println("数字:5 -- "+num[4]);
        System.out.println("数字:6 -- "+num[5]);
        System.out.println("数字:7 -- "+num[6]);
        System.out.println("数字:8 -- "+num[7]);
        System.out.println("数字:9 -- "+num[8]);
        System.out.println("数字:10 -- "+num[9]);
    }
}

题目5

  1. 模拟统计班级考试分数分布情况,分别统计100-80,79-60,59-40,39-0各个阶段的人数。
  • 定义getScoreList方法,随机生成50个数字,数字范围从0到100。

  • 定义countScore方法,统计各个阶段的分数个数。

  • 定义printCount方法,打印各个阶段的统计结果。

  • 代码实现,效果如图所示:
    在这里插入图片描述

代码:

public class Test05 {
    public static void main(String[] args) {
        ArrayList<Integer> scoreList = getScoreList();
        printCount(countScore(scoreList));
    }

    public static ArrayList<Integer> getScoreList(){
        ArrayList<Integer> arrayList = new ArrayList<>();
        Random random  = new Random();
        for(int i =0;i < 50;i++){
            arrayList.add(random.nextInt(101));
        }
        return arrayList;
     }

     public static int[] countScore(ArrayList<Integer> arrayList){
        Integer temp;
        int[] num = new int[4];
        for(int i =0;i<arrayList.size();i++){
                temp = arrayList.get(i);
                if(temp <=39){
                    num[0]++;
                }
                if(temp >=40 && temp <= 59){
                    num[1]++;

                }
                if(temp >= 60 && temp <=79){
                    num[2]++;
                }
                if(temp >= 80){
                    num[3]++;
                }
         }
        return num;
     }

    public static void printCount(int[] num){

        System.out.println("39  分 -- 0  分"+num[0]);
        System.out.println("59  分 -- 40 分"+num[1]);
        System.out.println("79  分 -- 60 分"+num[2]);
        System.out.println("100 分 -- 80 分"+num[3]);

    }
}

题目6

  1. 随机生成n张扑克牌。
  • 代码实现,效果如图所示:
    在这里插入图片描述

代码:

//牌类
public class Card {
    private String name;
    private String number;

    public Card() {
    }

    public Card(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}
//这个题目就是发牌的意思,就是随机多少张牌,就是发多少张牌
public class Test06 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要发的牌数");
        int n = scanner.nextInt();
        ArrayList<String> allCards = getAllCards();
        ArrayList<Card> randomCard = getRandomCard(allCards, n);
        System.out.println("随机"+n+"张牌:");
        for(int i =0;i < randomCard.size();i++){
            System.out.print(randomCard.get(i).getName()+randomCard.get(i).getNumber()+" ");
        }
    }

    //获取全部的牌数
    public static ArrayList<String> getAllCards(){
        ArrayList<String> arrayList = new ArrayList<>();
        String[] hs= new String[]{"方片","黑桃","红桃","梅花"};
        String[] ds= new String[]{"A","1","2","3","4","5","6","7","8","9","J","Q","K"};

        for(int i =0;i<hs.length;i++){
            for(int j =0;j< ds.length;j++){
                arrayList.add(hs[i]+ds[j]);
            }
        }

        return arrayList;
    }

//    发牌
    public static ArrayList<Card> getRandomCard(ArrayList<String> allcards,int n){
        ArrayList<Card> arraycardList = new ArrayList<>();
        Random random = new Random();
        if(n < 0 || n > allcards.size()){
            System.out.println("超越范围,无法获取");

        }else{
            for(int i = 0;i < n;i++){
                int i1 = random.nextInt(allcards.size());
                String s= allcards.get(i1);

                Card card = new Card(s.substring(0,1),s.substring(2));
                arraycardList.add(card);
                allcards.remove(i1);
            }
        }

        return arraycardList;
    }
}

题目7

  1. 自定义MyList类,实现存取元素的功能。
  • 定义add方法,可以保存元素,添加MyList尾部。
  • 定义remove方法,可以获取到最后添加的元素,并从MyList中移除该元素。
  • 定义show方法,可以展示MyList中的元素。
  • 代码实现,效果如图所示:
    在这里插入图片描述

代码:

public class MyList {
    private ArrayList<String> arrayList;

    public MyList() {
        this.arrayList = new ArrayList<String>();
    }

    public void add(String s){
        arrayList.add(s);
    }

    public String remove(){
        for(int i = 0;i< arrayList.size();i++) {
            if( i == arrayList.size() -1){
                return arrayList.remove(i);
            }
        }
        return null;
    }
    public void show(){
        System.out.print("[ ");
        for( int i = 0; i < arrayList.size(); i++){
            if(i == arrayList.size() -1){
                System.out.print(arrayList.get(i));
                break;
            }
            System.out.print(arrayList.get(i)+", ");
        }
        System.out.print("]");
    }
}
public class Test07 {
    public static void main(String[] args) {
        MyList myList = new MyList();
        myList.add("0");
        myList.add("1");
        myList.add("2");
        System.out.println("添加元素后:");
        myList.show();
        System.out.println(" ");
        System.out.println("获取元素:");
        System.out.println(myList.remove());
        System.out.println("获取元素后:");
        myList.show();

    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,我可以为您提供一个Java代码示例,实现生成100以内加法/减法的算式,批量产生习题并用文件存储,批改练习并存储。 首先,我们定义一个Exercise,用于生成习题和批改练习: ```java import java.util.ArrayList; import java.util.List; public class Exercise { private List<String> questions; private List<Integer> answers; public Exercise() { questions = new ArrayList<>(); answers = new ArrayList<>(); } public void generateQuestions(int count) { for (int i = 0; i < count; i++) { int a = (int) (Math.random() * 100); int b = (int) (Math.random() * 100); int op = (int) (Math.random() * 2); String question; int answer; if (op == 0) { question = a + " + " + b + " = "; answer = a + b; } else { question = a + " - " + b + " = "; answer = a - b; } questions.add(question); answers.add(answer); } } public List<String> getQuestions() { return questions; } public List<Integer> getAnswers() { return answers; } public int checkAnswers(List<Integer> userAnswers) { int correctCount = 0; for (int i = 0; i < answers.size(); i++) { if (answers.get(i).equals(userAnswers.get(i))) { correctCount++; } } return correctCount; } } ``` 这个有三个主要的方法: 1. `generateQuestions(int count)`:生成指定数量的习题,习题的数字在0到100之间随机生成,加减法随机选择,将习题和答案保存在内部的列表。 2. `checkAnswers(List<Integer> userAnswers)`:批改练习,将用户的答案列表作为参数传入,逐个比对答案并计算正确的数量。 3. `getQuestions()` 和 `getAnswers()`:用于获取习题和答案列表。 接下来,我们定义一个ExerciseFiles,用于批量产生并存储习题: ```java import java.io.FileWriter; import java.io.IOException; import java.util.List; public class ExerciseFiles { public static void generateFiles(String fileName, int count) throws IOException { Exercise exercise = new Exercise(); exercise.generateQuestions(count); FileWriter fileWriter = new FileWriter(fileName); List<String> questions = exercise.getQuestions(); for (String question : questions) { fileWriter.write(question + "\n"); } fileWriter.close(); } } ``` 这个有一个静态方法`generateFiles(String fileName, int count)`,用于生成指定数量的习题文件。它先生成习题,然后将习题按行写入指定文件。 现在,我们来编写一个示例程序,调用ExerciseFiles来生成习题文件,并调用Exercise来批改练习: ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); // 生成习题文件 System.out.print("请输入习题文件名:"); String fileName = scanner.nextLine(); System.out.print("请输入习题数量:"); int count = scanner.nextInt(); ExerciseFiles.generateFiles(fileName, count); System.out.println("习题文件已生成!"); // 读取习题文件 List<String> lines = Files.readAllLines(Paths.get(fileName)); List<Integer> userAnswers = new ArrayList<>(); for (String line : lines) { System.out.print(line); int answer = scanner.nextInt(); userAnswers.add(answer); } // 批改习题 Exercise exercise = new Exercise(); exercise.generateQuestions(count); int correctCount = exercise.checkAnswers(userAnswers); System.out.println("您的正确数量是:" + correctCount); } } ``` 这个程序会先让用户输入习题文件名和数量,然后调用ExerciseFiles生成习题文件。接着,它会读取习题文件,逐个打印习题并让用户输入答案,将用户的答案保存在列表。最后,它会调用Exercise批改习题并输出用户的正确数量。 这就是一个简单的Java代码示例,实现了生成100以内加法/减法的算式,批量产生习题并用文件存储,批改练习并存储。希望能对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值