zoukankan      html  css  js  c++  java
  • 四则运算随机生成

               这次题目要求是随机生成四则运算题目,缓存到文件中,然后显示在控制台中,由用户输入答案,然后电脑判断用户输入的结果是否正确,最后输出用户总共做对了几道题,做错了几道题。

    题目思路:

           首先,我们要随机生成四则运算的题目,每生成一道题目,就应该计算出它的答案,然后调用把题目传输到文件中的函数以及把答案传输到文件中的函数。然后在主函数中把文件中的题目通过输出函数显示在控制台上。用户每输入一个结果,就会自动显示下一个题目。同时正确答案存一个数组中,用户输入的答案存一个数组。然后判断用户输入的答案是否正确。以下是代码:

    package szys;
    
    import java.io.*;
    import java.util.Random;
    import java.util.Scanner;
    
    
    
    public class szys {
    
        public static void outartical(File file,String x[]) throws IOException 
        {
                Scanner input=new Scanner(System.in);
                StringBuilder result=new StringBuilder();
                String a=null;
                String b=null;
                String c=null;
                String d=null;
                int i=0;
                //构造一个BufferedReader类来读取文件
                BufferedReader br=new BufferedReader(new FileReader(file));
                String s=null;
                while((s=br.readLine())!=null)//使用readLine方法,一次读一行
                {
                    System.out.println("第"+(i+1)+"道题:");
                    System.out.println(s);
                    System.out.print("答案:");
                    x[i]=input.next();
                    i++;
                    System.out.println("**********");
                }
                br.close();
        }
        public static void outartical2(File file,String x[]) throws IOException 
        {
                Scanner input=new Scanner(System.in);
                StringBuilder result=new StringBuilder();
                String a=null;
                String b=null;
                String c=null;
                String d=null;
                int i=0;
                //构造一个BufferedReader类来读取文件
                BufferedReader br=new BufferedReader(new FileReader(file));
                String s=null;
                while((s=br.readLine())!=null)//使用readLine方法,一次读一行
                {
                    x[i]=s;
                    i++;
                }
                br.close();
        }
        
        public static void shuru (int l) throws IOException
        {
            FileWriter fw = null;
            FileWriter fd=null;
            try {
                //创建字符输出流
                fw = new FileWriter("poem.txt");
                fd=new FileWriter("daan.txt");
                
                int x;
                int y;
                int z;//随机产生1~4的数字
                //记录题目个数
                char s;//分别对应加减乘除
                String m=null;
                int b = 0;
                Random random=new Random();
               
                for(int i=1;i<=l;i++)
                {
                    x=0;y=0;z=0;
                    x=random.nextInt(100)+1;//随机产生1~100数字
                    y=random.nextInt(100)+1;//随机产生1~100数字
                    z=random.nextInt(4)+1;//随机产生1~4数字
                    switch(z)
                    {
                        case 1:m=x+"+"+y+"=";
                           b=x+y;
                           break;
                        case 2://System.out.println(x+"-"+y+"=");
                            ///break;
                         if(x>=y)
                         {
                             m=x+"-"+y+"=";
                             b=x-y;
                         }
                          else 
                              {
                                m=y+"-"+x+"=";
                                b=y-x;
                              }
                           
                        break;
                        case 3:m=x+"*"+y+"=";;b=x*y;
                        break;
                        case 4:m=x+"/"+y+"=";b=x/y;
                        break;
                    }
                  fw.write(m+"
    ");
                  fd.write(String.valueOf(b)+"
    ");
                }      
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                //使用finally块来关闭文件输出流
                if (fw != null&&fd!=null) 
                {
                    fw.close();
                    fd.close();
                 }
               }
        }
        
        public static void main(String []args) throws IOException {
            Scanner input=new Scanner(System.in);
            int l=0,k1=0,k2=0;
            String x[]=new String[150];
            String y[]=new String[150];
            int x1[]=new int[150];
            int y1[]=new int[150];
             System.out.println("题数:");
               l=input.nextInt();
            shuru(l);
            File file = new File("poem.txt");
            outartical(file,x);
            File file1 = new File("daan.txt");
            outartical2(file1,y);
            for(int i=0;i<l;i++)
            {
                x1[i]=Integer.parseInt(x[i]);
                y1[i]=Integer.parseInt(y[i]);
            }
            for(int i=0;i<l;i++)
            {
                if(x1[i]==y1[i])
                {
                    System.out.println("第"+(i+1)+"道题正确!");
                    k1++;
                }
                else
                {
                    System.out.println("第"+(i+1)+"道题错误!");
                    k2++;
                }
            }
            System.out.println("共答对"+k1+"道题!");
            System.out.println("共答错"+k2+"道题!");
        }    
    }
        

     完成时间:5点

    问题:文件的运用不熟练,导致大部分时间用来做对文件的操作了,而且总是出错。

  • 相关阅读:
    部署IIS HTTP 错误 500.19
    G2 V4 异步加载数据--Angular
    G2 V4 异步加载数据 分组柱状图X轴Y轴显示异常
    Angular G2 数据覆盖Y轴
    G2 RangeError: toFixed() digits argument must be between 0 and 100
    win7下users用户文件转移到其他盘符
    配置JDK和Tomcat环境变量
    oracle中exp,imp的使用详解
    c# 判断字符串中是否含有汉字,数字
    针对oracle集群的连接配置
  • 原文地址:https://www.cnblogs.com/liyuchao/p/9964861.html
Copyright © 2011-2022 走看看