Java课程课堂测试05

大致要求:将十道计算题输出至TXT文件,再读取文件至控制台,然后在控制台中输入答案并评判对错

  我在大致的要求当中已经将我的设计思路体现了出来

首先,实现计算题的设计,第二步要对计算题循环,将答案存到一个数组当中便于核对,然后利用文件流将计算题输出到文件中,然后再用文件流读到控制台然后,然后再在控制台中输入自己的答案和已经储存好的答案进行对比,并且判断对错。

package LessonTest;

import java.io.*;

public class four1 {
    
static    int []sum = new int[10];
    
    public void input() throws IOException
{
        int  nummin, nummax;
        String str1;
        String str2;
        char []ch= {'+','-','*','/'};
        int cha;

        File f = new File("C:/D/log.txt");
        FileOutputStream fop = new FileOutputStream(f);
        // 构建FileOutputStream对象,文件不存在会自动新建
 
        OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
        // 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk

        
        for(int i=0;i<10;i++)
        {
            nummax=(int)(Math.random()*1000);
            nummin=(int)(Math.random()*1000);

            cha=(int)(Math.random()*4);
             if(ch[cha]=='+'||ch[cha]=='-')
             {
                 if(ch[cha]=='+')
                     sum[i]=nummax+nummin;
                 else
                     sum[i]=nummax-nummin;
                 str1=Integer.toString(nummax);
                 str2=Integer.toString(nummin);
                 writer.append(str1);
                 writer.append(ch[cha]);
                 writer.append(str2);
                 writer.append("=");
            }
             else if(ch[cha]=='*')
             {
                 sum[i]=nummax*nummin;
                 nummax=(int)(Math.random()*100);
                 nummin=(int)(Math.random()*100);
                str1=Integer.toString(nummax);
                str2=Integer.toString(nummin);
                writer.append(str1);
                writer.append(ch[cha]);
                writer.append(str2);
                writer.append("=");
             }
             else if(ch[cha]=='/')
             {
                 nummin=(int)(Math.random()*10)+1;
                 while(nummax%nummin!=0)
                 {
                 nummax=(int)(Math.random()*1000);
                 nummin=(int)(Math.random()*10)+1;
                 }
                 sum[i]=nummax/nummin;
                str1=Integer.toString(nummax);
                str2=Integer.toString(nummin);
                writer.append(str1);
                writer.append(ch[cha]);
                writer.append(str2);
                writer.append("=");
             }


        // 写入到缓冲区
 
             writer.append("\r\n");
        // 换行
             
        }
            writer.close();
        
        // 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入

        // 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
        

        fop.close();
        // 关闭输出流,释放系统资源
 
        FileInputStream fip = new FileInputStream(f);
        // 构建FileInputStream对象
 
        InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
        // 构建InputStreamReader对象,编码与写入相同
 
        StringBuffer sb = new StringBuffer();
        while (reader.ready()) {
            sb.append((char) reader.read());
            // 转成char加到StringBuffer对象中
        }
        
        reader.close();
        // 关闭读取流
 
        fip.close();
        // 关闭输入流,释放系统资源

    }
}
package LessonTest;

import java.io.*;
import java.util.Scanner;

public class four2 
{

    public void name()throws Exception
    {
            String in=in("C:/D/log.txt");
            System.out.println(in);
    }
    private String in(String file) throws IOException {
        // TODO Auto-generated method stub
        int num;

        Scanner in=new Scanner(System.in);
        File f=new File(file);
        BufferedReader bf=new BufferedReader(new FileReader(f));
        String content="";
        for(int i=0;i<10;i++)
        {
            content=bf.readLine();
            System.out.println(content);
            num=in.nextInt();
            if(num==four1.sum[i])
                System.out.println("对");
            else 
                System.out.println("不对");
        }

        
        return null;
    }

}

 

package LessonTest;

import java.io.IOException;
import java.util.Scanner;

public class main {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        four1 f1=new four1();
        f1.input();
        four2 f2=new four2();
        f2.name();


    }

}

在课堂上没有做出来的原因,对于文件流的使用不是非常的熟练,所以导致没有很快的写出程序。

posted @ 2018-11-15 20:47  HEIYANG  阅读(186)  评论(0编辑  收藏  举报