四则运算

package CC;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.Vector;

public class GradeTwo {
    private static String opera = "+-*/";
    public static Vector<String> pro = new Vector<String>();
    public static Vector<String> key = new Vector<String>();
    public static void clear() {
        pro.clear();
        key.clear();
    }
    public static int isInteger(int c1, int c2, char opera) {
        int result = 0;
        int result1 = 0;
        switch (opera) {
        case '+':
            result = c1 + c2;
            break;
        case '-':
            result = c1 - c2;
            break;
        case '*':
            result = c1 * c2;
            break;
        case '/':
            result = c1 / c2;
            result1 = c1 % c2;
            break;
        }
        if (result >= 0 && result <= 100 && result1 == 0)
            return result;
        else
            return -1;
    }

    public static String getPoly() {
        String poly = "";
//        Random random = new Random();
//        
//        int c1;
//        int c2;
//        int result;
//        char operac;
//        boolean flag = true;
//        do {
//            c1 = random.nextInt(100) + 1;
//            c2 = random.nextInt(100) + 1;
//            operac = opera.charAt(random.nextInt(4));
//            if (operac == '*')
//                poly = c1 + " × " + c2 + " = ";
//            if (operac == '/')
//                poly = c1 + " ÷ " + c2 + " = ";
//            if (operac == '+')
//                poly = c1 + " + " + c2 + " = ";
//            if (operac == '-')
//                poly = c1 + " - " + c2 + " = ";
//            /*for (int i = 0;i<pro.size();++i) {
//                if (poly.equals(pro.get(i))) {
//                    flag = false;
//                    break;
//                }
//            }*/
//        } while (!flag || (result = isInteger(c1, c2, operac)) < 0);
//        pro.add(poly);
//        key.add(String.valueOf(result));
        Random random = new Random();
        //产生0.9的两位运算,0.1的三位运算
        char operate;
        char operate1;
        int operand1 = 0;
        int operand2 = 0;
        int operand3 = 0;
        int result;
        int percent = random.nextInt(100)+1;
        if(percent<=90) {
            operate = opera.charAt(random.nextInt(4));
            switch(operate) {
               case '+':
                   operand1 = random.nextInt(100);
                   operand2 = random.nextInt(100-operand1);
                   poly = operand1+" + "+operand2+" = ";
                   break;
               case '-':
                   operand1 = random.nextInt(100);
                   operand2 = random.nextInt(operand1+1);
                   poly = operand1+" - "+operand2+ " = ";
                   break;
               case '*':
                   operand1 = random.nextInt(9)+1;
                   operand2 = random.nextInt(9)+1;
                   poly = operand1+" × "+operand2+ " = ";
                   break;
               case '/':
                   operand1 = random.nextInt(9)+1;
                   operand2 = random.nextInt(9)+1;
                   operand1 = operand1*operand2;
                   poly = operand1+" ÷ "+operand2+ " = ";
                   break;
            }
            result = isInteger(operand1, operand2, operate);
            key.add(String.valueOf(result));
        }else {
            operate = opera.charAt(random.nextInt(2));
            operate1 = opera.charAt(random.nextInt(2));
            if(operate=='+') {
                operand1 = random.nextInt(100);
                operand2 = random.nextInt(100-operand1);
                if(operate1=='+') {
                    operand3 = random.nextInt(100-operand1-operand2);
                    poly = operand1+" + "+operand2+" + "+operand3+" = ";
                }else {
                    operand3 = random.nextInt(operand1+operand2);
                    poly = operand1+" + "+operand2+" - "+operand3+" = ";
                }
            }else {
                operand1 = random.nextInt(100);
                operand2 = random.nextInt(operand1);
                if(operate1=='+') {
                    operand3 = random.nextInt(100-operand1+operand2);
                    poly = operand1+" - "+operand2+" + "+operand3+" = ";
                }else {
                    operand3 = random.nextInt(operand1-operand2);
                    poly = operand1+" - "+operand2+" - "+operand3+" = ";
                }
            }
            result = isInteger(operand1, operand2, operate);
            result = isInteger(result, operand3, operate1);
            key.add(String.valueOf(result));
        }
        pro.add(poly);
        return poly;
    }
    public static int[]end(String[]list){
        int[]result = new int[3];
        result[0] = 0;
        result[1] = 0;
        result[2] = 0;
        int len = list.length;
        for(int i = 0;i<len;++i) {
            if(key.get(i).equals(list[i]))result[0]++;
            else {
                if(list[i].equals(""))result[2]++;
                else result[1]++;
            }
        }
        return result;
    }
    public static void main(String[]args) {
        int result = isInteger(55,13,'-');
        result = isInteger(result,19,'-');
        System.out.println(result);
    }
}
package Servlet;

import CC.GradeTwo;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class GradeServlet extends HttpServlet{
    public void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        String method = req.getParameter("method");
        if(method.equals("end"))end(req,resp); 
    }
    public void end(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
           String[]keys = req.getParameterValues("list");
           req.setAttribute("result",GradeTwo.end(keys));
           for(int i = 0;i<keys.length;++i)
               if(keys[i].equals(""))keys[i] = "未答";
           req.setAttribute("result1",keys);
           req.getRequestDispatcher("../result.jsp").forward(req, resp);
           return;
    }
}
原文地址:https://www.cnblogs.com/cc-9878/p/7994635.html