前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JavaSE 学习附加作业——IO 操作相关

JavaSE 学习附加作业——IO 操作相关

作者头像
剑影啸清寒
发布2019-05-26 10:07:42
4200
发布2019-05-26 10:07:42
举报
文章被收录于专栏:琦小虾的Binary琦小虾的Binary

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1434900

IO 操作相关作业

题目:

根据 coreJava.txt 文件:

  • 提炼出 Question 类
  • 解析文件(一个函数),要求:
代码语言:txt
复制
- 把同个等级的题目,放入同一个 ArrayList 集合;
- 把等级作为 key,把 ArrayList 集合作为 value,放入 Map 集合;出一套考题,每个等级一道题目(随机选出 Random)写成一个函数

部分 coreJava.txt 内容如下:

代码语言:javascript
复制
@answer=2/3,score=5,level=5
指出下面语句没有编译错误的是:
long n = 999999999999;
int n = 999999999999L;
long n = 999999999999L; 
double n = 999999999999;
@answer=1/2,score=5,level=4
下列关于数组的声明错误的是:
int[] arry = new int[100];
int[3] arry = {1,2,3};
int[] arry = new int[]{1,2,3};
int[][] arry = new int[3][];
@answer=0/1/2,score=5,level=3
语句System.out.println(1+2+"java"+3+4)输出的结果是:
3java34
12java34
3java7
12java7
@answer=1,score=5,level=6
Java语言中int类型标示整数的最大范围是:
-2147483647~ 2147483647 
<T>-2147483648 ~2147483647 
-32767~32767
-32768~32767
@answer=1,score=5,level=7
Java语言中字符串“学Java”所占的内存空间是:
6个字节
7个字节
10个字节
11个字节
@answer=1,score=5,level=8
对JDK描述错误的是:
运行Java程序只要有JRE环境就可以,不一定需要全部JDK的功能
JDK中包括JVM、核心类库、Java开发工具(java,javac)等
JDK本身也是平台无关的,不同的操作系统安装的JDK是一样的
JDK的全称是 Java Development Kit

答案:

1. Question.java

作用:提炼出 Question 类

代码语言:javascript
复制
package homework4_27;

import java.util.ArrayList;

public class Question {
    private int score;
    private int level;
    private String answer;
    private String description;
    private ArrayList<String> options;

    public Question() {
        this.options = new ArrayList<String>();
    }
    public Question(int score, int level, String answer, String description) {
        super();
        this.score = score;
        this.level = level;
        this.answer = answer;
        this.description = description;
        this.options = new ArrayList<String>();
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public int getLevel() {
        return level;
    }
    public void setLevel(int level) {
        this.level = level;
    }
    public String getAnswer() {
        return answer;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public ArrayList<String> getoptions() {
        return options;
    }

    public void addOption(String op) {
        options.add(op);
    }
    @Override
    public String toString() {
        return "Question [score=" + score + ", level=" + level + ", answer=" + answer + ", description=" + description
                + ", options=" + options + "]";
    }

    public void showQuestion() {
        System.out.println("Question: " + description);
        System.out.println("Options: ");
        for(String op : options) {
            System.out.println(op);
        }
        System.out.println("Answer: " + answer);
        System.out.println("Score: " + score);
    }
}

2. homework2.java

作用

  • 解析文件:
代码语言:txt
复制
- 把同个等级的题目,放入同一个 ArrayList 集合;
- 把等级作为 key,把 ArrayList 集合作为 value,放入 Map 集合;
代码语言:javascript
复制
package homework4_27;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

// 解析 v2.0,将所有解析后的问题放入单一一个 ArrayList 中
public class homework2 {
    public static void main(String[] args) {
        // 按 level 存入的问题集
        HashMap<Integer, ArrayList<Question> > questWithLevel = new HashMap<Integer, ArrayList<Question> >();

        try {
            BufferedReader br = 
                    new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream("/Users/upcautolang/Documents/corejava.txt")));
            String str = null;

            Question question = null;
            int extraLineNum = 0;
            while((str = br.readLine()) != null){
                // 包含 @,则可以分析问题的学分、答案、等级
                if(str.contains("@")) {
                    // 将当前非空的 question 传入 questWithLevel 的 Map 集合
                    if(question != null) {
                        int key = question.getLevel();
                        ArrayList<Question> tmpList = new ArrayList<Question>();
                        if(questWithLevel.containsKey(key)) {
                            tmpList = questWithLevel.get(key);
                        }
                        else {
                            tmpList = new ArrayList<Question>();
                        }
                        tmpList.add(question);
                        questWithLevel.put(key, tmpList);
                    }
                    question = new Question();
                    // 切分信息
                    // 例:@answer=2/3,score=5,level=5
                    String[] infos = str.split("[@,][a-z]+=");
                    // 设置答案,成绩,等级
                    question.setAnswer(infos[1]);
                    question.setScore(Integer.parseInt(infos[2]));
                    question.setLevel(Integer.parseInt(infos[3]));

                    extraLineNum = 0; 
                }
                else {
                    if(extraLineNum == 0) {
                        question.setDescription(str);
                        extraLineNum = 1;
                    }
                    else {
                        question.addOption(str);
                        extraLineNum++;
                    }
                }
            }
            // 测试打印
            for(int level : questWithLevel.keySet()) {
                System.out.println("================================");
                System.out.println("Level " + level + " Question Set:");
                System.out.println("---------");
                ArrayList<Question> questionList = questWithLevel.get(level);
                for(Question quest : questionList) {
                    System.out.println(quest);
                }
            }

        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

3. homework3.java

作用:出一套考题,每个等级一道题目(随机选出 Random)写成一个函数

代码语言:javascript
复制
package homework4_27;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

// 解析 v2.0,将所有解析后的问题放入单一一个 ArrayList 中
public class homework3 {
    public static void main(String[] args) {
        // 按 level 存入的问题集
        HashMap<Integer, ArrayList<Question> > questWithLevel = new HashMap<Integer, ArrayList<Question> >();

        try {
            BufferedReader br = 
                    new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream("/Users/upcautolang/Documents/corejava.txt")));
            String str = null;

            Question question = null;
            int extraLineNum = 0;
            while((str = br.readLine())!=null){
                // 包含 @,则可以分析问题的学分、答案、等级
                if(str.contains("@")) {
                    // 将当前非空的 question 传入 questWithLevel 的 Map 集合
                    if(question != null) {
                        int key = question.getLevel();
                        ArrayList<Question> tmpList = new ArrayList<Question>();
                        if(questWithLevel.containsKey(key)) {
                            tmpList = questWithLevel.get(key);
                        }
                        else {
                            tmpList = new ArrayList<Question>();
                        }
                        tmpList.add(question);
                        questWithLevel.put(key, tmpList);
                    }
                    question = new Question();
                    // 切分信息
                    // 例:@answer=2/3,score=5,level=5
                    String[] infos = str.split("[@,][a-z]+=");
                    // 设置答案,成绩,等级
                    question.setAnswer(infos[1]);
                    question.setScore(Integer.parseInt(infos[2]));
                    question.setLevel(Integer.parseInt(infos[3]));

                    extraLineNum = 0; 
                }
                else {
                    if(extraLineNum == 0) {
                        question.setDescription(str);
                        extraLineNum = 1;
                    }
                    else {
                        question.addOption(str);
                        extraLineNum++;
                    }
                }
            }

            // 随机出题
            System.out.println("=========================");
            System.out.println("     琦少的随机考试");
            System.out.println("=========================");

            for(int level : questWithLevel.keySet()) {
                System.out.println("------- Level " + level + " --------");
                ArrayList<Question> questionList = questWithLevel.get(level);
//              int randomNumber = (int) Math.round(Math.random() * (questionList.size()-1));
                int randomNumber = new Random().nextInt(questionList.size());
                questionList.get(randomNumber).showQuestion();
            }
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年06月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • IO 操作相关作业
  • 答案:
    • 1. Question.java
      • 2. homework2.java
        • 3. homework3.java
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档