设计思想:
写一个生成四则运算的java类,封装起来,以便调用。四则运算的符号用死以内的随机数生成,1对应+,以此类推,用if语句控制结果在100以内。
开发中遇到的问题:
写三元运算的时候,把二元运算的代码直接复制了下来,结果只有两个操作数。
解决的方法:
重新写了代码,生成三元运算。
源代码:
public class BinaryAirthmetic { private String firstOperation; private String secondOperation; private String character1; private String character2; public String getCharacter2() { return character2; } public void setCharacter2(String character2) { this.character2 = character2; } private int first; private int second; private int mid; public int getMid() { return mid; } public void setMid(int mid) { this.mid = mid; } private String midOperation; public String getMidOperation() { return midOperation; } public void setMidOperation(String midOperation) { this.midOperation = midOperation; } public String getFirstOperation() { return firstOperation; } public void setFirstOperation(String firstOperation) { this.firstOperation = firstOperation; } public String getSecondOperation() { return secondOperation; } public void setSecondOperation(String secondOperation) { this.secondOperation = secondOperation; } public String getCharacter1() { return character1; } public void setCharacter1(String character1) { this.character1 = character1; } public int getFirst() { return first; } public void setFirst(int first) { this.first = first; } public int getSecond() { return second; } public void setSecond(int second) { this.second = second; } private int result; public int getResult() { return result; } public void setResult(int result) { this.result = result; }}
mport java.util.ArrayList;import java.util.List;import java.util.Random;import BinaryAirthmetic.BinaryAirthmetic;public class MathProblem { public static ListBinary(int num,int yuan) { List binaryAirthmetics=new ArrayList<>(); Random random=new Random(); int number=1; int left=0; int mid=0; int right=0; int c=0; int c1; int result=0; String output=""; String output1=""; if(yuan==2) { for ( int i = 1; i <= 5000; i++ ) { BinaryAirthmetic binaryAirthmetic=null; // left = 1+random.nextInt(20); // right=1+random.nextInt(20); c=1 + (int) ( Math.random() * 4 ); switch(c){ case 1:{ left = 1+random.nextInt(20); right=1+random.nextInt(20); output="+";result=left+right;break;} case 2:{ left = 1+random.nextInt(20); right=1+random.nextInt(20); output="-";result=left-right;break;} case 3:{ left = 1+random.nextInt(9); right=1+random.nextInt(9); output="×";result=left*right;break;} case 4:{ left = 1+random.nextInt(9); right=1+random.nextInt(9); output="÷";result=left/right;break;} } if(result<=100&&number<=num&&result>=0&&(left%right==0)){ number++; binaryAirthmetic=new BinaryAirthmetic(); binaryAirthmetic.setFirst(left); binaryAirthmetic.setSecond(right); binaryAirthmetic.setCharacter1(output); binaryAirthmetic.setResult(result); binaryAirthmetics.add(binaryAirthmetic); } } }else { for ( int i = 1; i <= 5000; i++ ) { BinaryAirthmetic binaryAirthmetic=null; left = 1+random.nextInt(20); mid=1+random.nextInt(20); right=1+random.nextInt(20); c=1 + (int) ( Math.random() * 2 ); switch(c){ case 1:{output="+";break;} case 2:{output="-";break;} } c1=1 + (int) ( Math.random() * 2 ); switch(c1){ case 1:{output1="+";break;} case 2:{output1="-";break;} } if(c==1&&c1==1) { result=left+mid+right; } if(c==1&&c1==2) { result=left+mid-right; } if(c==2&&c1==1) { result=left-mid+right; } if(c==2&&c1==2) { result=left-mid-right; } if(result<=100&&number<=num&&result>=0&&(left-mid)>=0){ number++; binaryAirthmetic=new BinaryAirthmetic(); binaryAirthmetic.setFirst(left); binaryAirthmetic.setMid(mid); binaryAirthmetic.setSecond(right); binaryAirthmetic.setCharacter1(output); binaryAirthmetic.setCharacter2(output1); binaryAirthmetic.setResult(result); binaryAirthmetics.add(binaryAirthmetic); } } } return binaryAirthmetics;}}
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><% session.setAttribute("time", 10); %>选择界面
<%@page import="java.util.List"%><%@page import="BinaryAirthmetic.BinaryAirthmetic"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%char c='a';int i=1;int yuan=(int)session.getAttribute("yuan");String string1="对";String string2="错";String s;String string;ListbinaryAirthmetics=(List )session.getAttribute("binary");%> 判断界面 <% if(yuan==2){ for(BinaryAirthmetic binaryAirthmetic:binaryAirthmetics) { //s=String.valueOf(c); string=Integer.toString(i, 1000); if("".equals(request.getParameter(string).trim())) { %>
<%i++;continue; } %> <% if(binaryAirthmetic.getResult()==Integer.parseInt(request.getParameter(string).trim())){%> <% out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的结果:无"); %> <% }else{ %> <% out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的答案:"+request.getParameter(string)+"结果:正确"); %> <% }%><%c++;i++; } } %> <% if(yuan==3){ for(BinaryAirthmetic binaryAirthmetic:binaryAirthmetics) { //s=String.valueOf(c); string=Integer.toString(i, 1000); if("".equals(request.getParameter(string).trim())) { %> <%out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的答案:"+request.getParameter(string)+"结果:错误"); %> <%i++;continue; } %> <% if(binaryAirthmetic.getResult()==Integer.parseInt(request.getParameter(string).trim())){%> <% out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的结果:无"); %> <% }else{ %> <% out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的答案:"+request.getParameter(string)+"结果:正确"); %> <% }%><%c++;i++; } } %> <% if(yuan!=2&&yuan !=3){ out.print("请输入2或者3"); } %> <%out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的答案:"+request.getParameter(string)+"结果:错误"); %>
<%@page import="javax.websocket.Session"%><%@page import="Database.DatabaseConnection"%><%@page import="BinaryAirthmetic.BinaryAirthmetic"%><%@page import="java.util.List"%><%@page import="MathProblem.MathProblem"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%int num=Integer.parseInt(request.getParameter("num"));int yuan=Integer.parseInt(request.getParameter("yuan"));int i=1;int number=1;MathProblem mathProblem=new MathProblem();ListbinaryAirthmetics=mathProblem.Binary(num, yuan); DatabaseConnection databaseConnection=new DatabaseConnection(); databaseConnection.addDatabase(num, yuan,binaryAirthmetics); char a='a'; session.setAttribute("binary", binaryAirthmetics); session.setAttribute("yuan", yuan); int time=(int)session.getAttribute("time");%> 答题界面 时间还剩下: 0分钟。10分钟秒后系统自动提交。
运行结果截图: