博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
四则运算1
阅读量:6187 次
发布时间:2019-06-21

本文共 9668 字,大约阅读时间需要 32 分钟。

设计思想:

写一个生成四则运算的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 List
Binary(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); %>选择界面
选择题的数目(范围1~100)
 选择几元运算(范围2~3)
<%@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;List
binaryAirthmetics=(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())){%>
<% }else{ %>
<% }%><%c++;i++; } } %> <% if(yuan==3){ 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())){%>
<% }else{ %>
<% }%><%c++;i++; } } %> <% if(yuan!=2&&yuan !=3){ out.print("请输入2或者3"); } %>
<% out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的结果:无"); %>
<% out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的答案:"+request.getParameter(string)+"结果:正确"); %>
<%out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的答案:"+request.getParameter(string)+"结果:错误"); %>
<% out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的结果:无"); %>
<% out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); out.println("你的答案:"+request.getParameter(string)+"结果:正确"); %>
<%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();List
binaryAirthmetics=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分钟秒后系统自动提交。
<% if(yuan==2){ for(BinaryAirthmetic binaryAirthmetic:binaryAirthmetics) { String s=Integer.toString(i, 1000);%>
<% a++;i++;number++; } %> <% }%><% if(yuan==3){ for(BinaryAirthmetic binaryAirthmetic:binaryAirthmetics) { String s=Integer.toString(i, 100);%>
<% a++;i++;number++; } %> <% }%>
<%=number+"."+"\r\r"+binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"=" %> >
<%=number+"."+"\r\r"+binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"=" %> >
<%if(yuan!=2&&yuan!=3){ out.print("元数请选择二元或者三元"); }%>

运行结果截图:

 

 

转载于:https://www.cnblogs.com/andibier/p/7995001.html

你可能感兴趣的文章
python爬虫系列(3.2-lxml库的使用)
查看>>
SEO提高网站排名快速见效的方法
查看>>
(十五) 构建springmvc+mybatis+dubbo分布式平台-window安装dubbo管控台
查看>>
Mvp官方示例
查看>>
windows下安装mysql5.7 (爬过多次坑)总结
查看>>
两人一组,注册账号密码,注册COOKIE是否能够登陆?
查看>>
Object-C中使用NSKeyedArchiver归档(将各种类型的对象存储到文件中)
查看>>
一位大牛整理的python资源
查看>>
设计模式 单例模式(Singleton)
查看>>
jqurey 隐藏
查看>>
Cisco Eigrp SIA 理解
查看>>
虚拟机虚拟网卡作用
查看>>
我的友情链接
查看>>
如何做好一个项目经理
查看>>
Tex之版面布局设计
查看>>
Android调用系统照相机拍摄视频并将其拷贝到制定的文件夹下
查看>>
php的sso单点登录实现方法
查看>>
javascript eval和JSON之间的联系
查看>>
iOS沙盒(sandbox)机制及获取沙盒路径
查看>>
Jean同学的Proguard私房物语
查看>>