每日总结
今天上了软件构造。
package com.itheima.test; import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; public class Test1 { public static void main(String[] args) { List<Question> questionList=setQuestion(); //System.out.println(questionList.size()); List<Question> cuoti=printQuestion(questionList); cuoti(cuoti); } /* * 进行题目生成和结果的生成并进行题目重复的判断。 * 返回值为List<Question> 使用类接收题目和结果。 * * */ public static List<Question> setQuestion(){ Scanner scanner = new Scanner(System.in); List<Question> questions=new ArrayList<>(); List<Calculate> calculates=new ArrayList<>(); int first = 0; int str= 0; int flag = 0; int second = 0; int bound=2; System.out.println("请输入多少以内的算数"); int fanwei=scanner.nextInt(); System.out.println("是否包含乘除法"); flag=scanner.nextInt(); if (flag==0){ bound=2; }else { bound=4; } System.out.print("输入题数"); int num=scanner.nextInt(); String [] strings = new String[num]; for (int i=0;i<num;i++){ Question question=new Question(); Random random=new Random(); Calculate calculate=new Calculate(); //随机数取值 first= random.nextInt(fanwei)+1; str =random.nextInt(bound); second=random.nextInt(fanwei)+1; //对calculate对象赋值 calculate.setFirst(first); calculate.setSecond(second); setCalculate(calculate,str); //题目字符串存入question做准备 String s =i+1+":\t"+calculate.toString(); if (checks(calculate,calculates)){ //满足不重复条件写入list calculates.add(calculate); strings[i] = s; question.setQues(s); question.setResult(calculate.getResult()); questions.add(question); }else { i--; } } return questions; } /* * 进行题目问答输出题目 * 返回值为List<Question> 错题集 * */ public static List<Question> printQuestion(List<Question> questions){ Scanner scanner = new Scanner(System.in); int num=0; int cc=0; List<Question> stringList =new ArrayList<>(); for (int i =0;i<questions.size();i++){ System.out.print(questions.get(i).getQues()); int res = scanner.nextInt(); if (res==questions.get(i).getResult()){ num++; }else { cc++; stringList.add(questions.get(i)); } } System.out.println("你答对了"+num+"题,答错了"+cc+"题"); return stringList; } /* * 进行重复判断 * */ public static boolean checks(Calculate calculate ,List<Calculate> calculates){ for (int i =0;i< calculates.size();i++){ if (calculate.equals(calculates.get(i))){ return false; } } return true; } /* * 判断是否含有错题 * 并判断是否进行错题重练 * */ public static void cuoti(List<Question> cuoti){ Scanner scanner = new Scanner(System.in); if (cuoti.size()==0){ System.out.println("恭喜你全对了"); }else { System.out.println("是否进行错题重练"); int flag = scanner.nextInt(); //错题重练 if (flag==1){ printQuestion(cuoti); } else return; } } /* * 进行算式的生成 * 并记录结果存入calculate对象 * */ public static Calculate setCalculate(Calculate calculate,int str){ if (str == 0){ calculate.setStr("-"); if (calculate.getFirst()>calculate.getSecond()){ calculate.setResult(calculate.getFirst()-calculate.getSecond()); }else { int first=calculate.getFirst(); calculate.setFirst(calculate.getSecond()); calculate.setSecond(first); calculate.setResult(calculate.getFirst()-calculate.getSecond()); } }else if (str==1){ calculate.setStr("+"); calculate.setResult(calculate.getFirst()+calculate.getSecond()); } else if (str==2){ calculate.setStr("*"); calculate.setResult(calculate.getFirst()*calculate.getSecond()); }else { calculate.setStr("/"); calculate.setResult(calculate.getFirst()/calculate.getSecond()); } return calculate; } }
继续完善了之前的题卡程序