四则运算1

设计思想:

写一个生成四则运算的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<BinaryAirthmetic> Binary(int num,int yuan) {
        List<BinaryAirthmetic> 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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<% session.setAttribute("time", 10); %>
<head>

<title>选择界面</title>
</head>
<body style="background: url(timg.jpg)"  background-size:cover; >
<font size="10">
<center>
  <form action="list.jsp" method="get">
     <table>
 <br>
 <br>
 <br>
 <br>
       <tr> <th>选择题的数目(范围1~100) <input type="text" name="num" />        </th>       </tr>
       <tr> <th>&nbsp;选择几元运算(范围2~3) <input type="text" name="yuan" />        </th>       </tr>
       
      <tr> <th> <input type="submit" value="开始答题" />  <input type="reset" value="重置" />      </th>       </tr>
     
     </table> 
      </center>
  </form>
</font>
</body>
</html>
<%@page import="java.util.List"%>
<%@page import="BinaryAirthmetic.BinaryAirthmetic"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
char c='a';
int i=1;
int yuan=(int)session.getAttribute("yuan");
String string1="对";
String string2="错";
String s;
String string;
List<BinaryAirthmetic> binaryAirthmetics=(List<BinaryAirthmetic>)session.getAttribute("binary");
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>判断界面</title>
</head>
 <body   background-size:cover;>
 <font size="5">
<center>
<table>
      <%
      if(yuan==2){
   for(BinaryAirthmetic binaryAirthmetic:binaryAirthmetics) {
      
       //s=String.valueOf(c);
       string=Integer.toString(i, 1000);
      if("".equals(request.getParameter(string).trim()))
      {
      
        %>
<tr>
<th> 
     <%
     out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); 
     out.println("你的结果:无");
     %>
    
     
</th>
</tr>
<%
i++;
continue;

   }
      %>
      <% 
if(binaryAirthmetic.getResult()==Integer.parseInt(request.getParameter(string).trim())){
%>
<tr>
  <th> 
     <%
     out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); 
     out.println("你的答案:"+request.getParameter(string)+"结果:正确"); 
     
     %>
  </th>

</tr>

<%     
}else{
    %>
        <tr>
      <th> 
         <%out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); 
         out.println("你的答案:"+request.getParameter(string)+"结果:错误"); %>
      </th>

    </tr>

    <%         
}
%>
<%
c++;
i++;
   }   
      }
     %>
      <%
      if(yuan==3){
   for(BinaryAirthmetic binaryAirthmetic:binaryAirthmetics) {
      
       //s=String.valueOf(c);
       string=Integer.toString(i, 1000);
      if("".equals(request.getParameter(string).trim()))
      {
      
        %>
<tr>
<th> 
     <%
     out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); 
     out.println("你的结果:无");
     %>
    
     
</th>
</tr>
<%
i++;
continue;

   }
      %>
      <% 
if(binaryAirthmetic.getResult()==Integer.parseInt(request.getParameter(string).trim())){
%>
<tr>
  <th> 
     <%
     out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); 
     out.println("你的答案:"+request.getParameter(string)+"结果:正确"); 
     
     %>
  </th>

</tr>

<%     
}else{
    %>
        <tr>
      <th> 
         <%out.println(binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"="+binaryAirthmetic.getResult()); 
         out.println("你的答案:"+request.getParameter(string)+"结果:错误"); %>
      </th>

    </tr>

    <%         
}
%>
<%
c++;
i++;
   }   
      }
     %>
     <%
     if(yuan!=2&&yuan !=3){
         out.print("请输入2或者3");
     }
     %>
   </center>
</table>
</font>
</body>
</html>
<%@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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<%
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<BinaryAirthmetic> 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");
%>
 
<head>

<title>答题界面</title>
</head>
时间还剩下:<span id=id2>0</span>分钟。10分钟秒后系统自动提交。
<script language="javascript">
var i=11;
function showtime(){
  i=i-1;
  //time=time-1;
  id2.innerHTML=i;
  setTimeout("showtime()",1000);
  if(i==0)
    document.form1.submit();
}
showtime();
</script>
 <body   background-size:cover; οnlοad="shownum()">
 <font size="5">
<form action="judge.jsp" method="get" name="form1">
<center>
<table >

<%   if(yuan==2){
   for(BinaryAirthmetic binaryAirthmetic:binaryAirthmetics) {
      
      String s=Integer.toString(i, 1000);
%>
<tr>

<th> <%=number+"."+"\r\r"+binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getSecond()+"=" %> <input/ type="text" name=<%=s %>> </th>

</tr>

<% 
a++;
i++;
number++;
   }    
   %>
   <% 
   }
%>
<%   if(yuan==3){
   for(BinaryAirthmetic binaryAirthmetic:binaryAirthmetics) {
       String s=Integer.toString(i, 100);
%>
<tr>

<th> <%=number+"."+"\r\r"+binaryAirthmetic.getFirst()+binaryAirthmetic.getCharacter1()+binaryAirthmetic.getMid()+binaryAirthmetic.getCharacter2()+binaryAirthmetic.getSecond()+"=" %> <input/ type="text" name=<%=s %>> </th>

</tr>

<% 
a++;i++;number++;
   }    
   %>
   <%
   }
%>
<tr>
<th>
<%
if(yuan!=2&&yuan!=3){
    out.print("元数请选择二元或者三元");
    
}
%>
<input type="submit" name="提交答案"  />
<input type="reset" name="重置"  />
</th>

</tr>
</table>

</center>
</font>
</body>
</html>

运行结果截图:

 

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值