java实验(三)——课堂小测

这次的课堂小测是用以前生成的那些四则运算的代码,然后将这些题目写到一个文件中,再通过这个文件读取题目的信息,每读入一个答案的时候,遇到星号的时候,等待用户输入然后判断输入的答案是否正确,然后输出小一道题目让用户输入,直到文件读到结束的位置。

思路大概是如下:

一开始的思路是答案同样也在文本输入,例如1+1=2*;

每次读取到等号的时候,将文件中答案读取出来,并且将变量设置为此值。接下来就能用户输入的答案是否正确了。

//Sizezt.java的代码如下:

package sz;

//import java.io.BufferedReader;
import java.io.File;
//import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Vector;





public class Sizezt {


    //出现运算项的最高个数
    private static final int SXSIZE=4;
    //出多少道题目
    private static final int TIMES=100;
    private static Vector<Integer> ve=new Vector<Integer>();
    public static Vector<Integer> getVe() {
        return ve;
    }
    public static void setVe(Vector<Integer> ve) {
        Sizezt.ve = ve;
    }
    public static Vector<String> getSe() {
        return se;
    }
    public static void setSe(Vector<String> se) {
        Sizezt.se = se;
    }
    private static Vector<String> se=new Vector<String>();
    public static void run() {
        // TODO 自动生成的方法存根
        ve.clear();
        se.clear();
        int times=(int)(Math.random()*(SXSIZE-1)+1);
        String szM[]=new String[]{"+","-","X","/"};
        int numa=0,numb=0,numc=0,numg=0;
        String se_c="";
        int type=0;
        for(int j=0;j<TIMES;j++)
        {
            boolean g_ok=false;
            boolean g_run=false;
            boolean g_list=false;
            boolean g_xiuzheng=false;
            numg=0;
            se_c="";
            for(int i=0;i<times;i++)
            {

                if(g_ok&&isOk(numa)||g_ok&&isOk(numc))
                {
                    se_c+="=";
                    System.out.print("="+"
");
                    ve.add(numc);
                    numc=-2000;
                    break;
                }
                if(!g_ok)
                {
                    numa=(int)(Math.random()*600+1);
                    numb=(int)(Math.random()*600+1);
                    if(numg==0)
                        numg=numa;
                }
                type=!g_list?(int)(Math.random()*4+1):(int)(Math.random()*2+1);
                //System.out.print("	"+type+"	");
                if(type==4)
                {
                    if(!g_ok)
                    {
                        while(numa%numb!=0||numb==1)
                        {
                            numa=(int)(Math.random()*600+1);
                            numb=(int)(Math.random()*600+1);
                        }
                        numc=numa/numb;
                    }
                    else
                    {
                        while(numg%numb!=0)
                        {
                            numb=(int)(Math.random()*600+1);
                        }
                        numc-=g_xiuzheng?-numg:numg;
                        if(!g_xiuzheng)
                            numc+=numg/numb;
                        else numc-=numg/numb;

                    }
                    g_list=true;
                    g_run=!g_run;
                }
                else if(type==3)
                {

                    if(!g_ok)
                    {
                        while(numa*numb>1000||numa==1||numb==1)
                        {
                            numa=(int)(Math.random()*600+1);
                            numb=(int)(Math.random()*600+1);
                        }
                        numc=numa*numb;
                    }
                    else
                    {
                        while(numg*numb>1000||numb!=1)
                        {
                            numb=(int)(Math.random()*600+1);
                        }
                        numc-=g_xiuzheng?numg:numg;
                        numc+=numg*numb;
                    }
                    g_list=true;
                }

                else if(type==2)
                {
                    if(!g_ok)
                        numc=numa-numb;
                    else numc-=numb;
                    g_list=false;
                }
                else if(type==1)
                {
                    if(!g_ok)
                        numc=numa+numb;
                    else numc+=numb;
                    g_list=false;
                }

                if(!g_ok)
                {
                    System.out.print(numa+szM[type-1]+numb);
                    se_c+=numa+szM[type-1]+numb;
                }
                else
                {
                    System.out.print(szM[(type==4&&!g_run)?(int)(Math.random()*2):type-1]+numb);
                    se_c+=szM[(type==4&&!g_run)?(int)(Math.random()*2):type-1]+numb;
                }
                g_ok=true;
                if(type==2)
                    g_xiuzheng=true;
                else g_xiuzheng=false;
                numg=numb;
                numb=(int)(Math.random()*600+1);
            }
            if(numc!=-2000)
            {
                ve.add(numc);
                System.out.print("="+"
");
                se_c+="=";
            }


            se.add(se_c);
            numc=0;
            times=(int)(Math.random()*(SXSIZE-1)+1);
        }



        //            Document document = null;
        //            try {
        //                document = XmlUtils.getDocument();
        //            } catch (DocumentException e) {
        //                // TODO 自动生成的 catch 块
        //                e.printStackTrace();
        //            }
        //            Element root = document.getRootElement();
        //            Element user_node = root.addElement("sz");  //创建user结点,并挂到root
        //            
        //            //user_node.setAttributeValue("ve", ve);

    }
    public static boolean isOk(int num)
    {
        if(num==1||num<=0||num==2)
            return false;
        for(int i=2;i<=Math.sqrt(num);i++)
        {
            if(num%i==0)
                return false;
        }
        return true;
    }
    public void daochu() throws IOException
    {
        String st="";
        int an=0;
        Iterator<String> it;
        Iterator<Integer> it1;
        File a=new File("timu.txt");
        FileOutputStream b = new FileOutputStream(a);
        OutputStreamWriter c=new OutputStreamWriter(b,"UTF-8");
        it=se.iterator();
        it1=ve.iterator();
        int i=0;
        while(it.hasNext())
        {
            i++;
            if(i!=1)
                c.append("
");
            st=it.next();
            an=it1.next();
            c.append(st+an+"*");
            
        }
        c.close();
        b.close();
        
        
    }
    public static void main(String args[])
    {
        Sizezt.run();
        Sizezt a=new Sizezt();
        try {
            a.daochu();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        Daoru b=new Daoru();
        try {
            b.daoru();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    
    


}

导入类我单独写了一个,方便文件的导入。

//Daoru.java的代码如下所示:

package sz;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Vector;

public class Daoru {

    Scanner can=new Scanner(System.in);
    public boolean isNum(char a)
    {
        if(a-'0'>=0&&a-'0'<=9)
            return true;
        return false;
    }
    public void daoru() throws IOException
    {
        
        File a=new File("timu.txt");
        FileInputStream b = new FileInputStream(a);
        InputStreamReader c=new InputStreamReader(b,"UTF-8");
//        {
//            BufferedReader bufr =new BufferedReader(c);
//            String line = null;
//            while((line = bufr.readLine())!=null){
//                Student st=new Student();
//                String ook[]=line.toUpperCase().split(" ");
//                st.set(ook[0],ook[1],ook[2]);
//                map.add(st);
//            }
//            bufr.close();
//            //System.out.println(sb.toString());
//            
//        }
        int num=0,count=0,account=0;
        Vector<Integer> num1=new Vector<Integer>();
        int g_score=0;
        boolean g_deng=false,g_xin=true,g_jian=false;
        while(c.ready())
        {
            char sz=(char)c.read();
            if(g_xin)
            {
                ++account;
                System.out.print("
第"+account+"题目:");
                if(account==1)
                {
                    System.out.print("
");
                }
                g_xin=false;
            }
            if(isNum(sz)&&g_deng)
            {
        
                num1.add((int)(sz-'0'));
                count++;
            }
            else if(sz=='=')
            {
                num1.clear();
                num=0;
                count=0;
                g_deng=true;
                System.out.print(sz);
            }
            else if(sz=='*')
            {
                for(int i=num1.size()-1;i>=0;i--)
                {
                    num+=num1.get(num1.size()-1-i)*Math.pow(10, i);
                }
                
                if(g_jian)
                    num=-num;
                //System.out.print(num);
                num1.clear();
                g_deng=false;
                g_xin=true;
                g_jian=false;
                
                boolean g_down=true;
                while(g_down)
                {
                    if(can.hasNextInt())
                    {
                        
                        int daan=can.nextInt();
                        
                        if(daan==num)
                        {
                            g_score++;
                        }
                        g_down=false;
                    }
                    else
                    {
                        can.next();
                        System.out.println("
格式错误,请重新输入");
                    }
                }
                num=0;
                count=0;
            }
            else if(sz=='-'&&g_deng)
            {
                g_jian=true;
                
            }
            else
            {
                System.out.print(sz);
            }
        }
        c.close();
        b.close();
        System.out.println("
你最终的得分是:"+g_score);
    }
    
    
}

生成的size.txt文件如下所示

实验的截图如下:

 此次的实验大概就是如此

原文地址:https://www.cnblogs.com/halone/p/9966220.html