zoukankan      html  css  js  c++  java
  • 四则运算1.1版

    package Better;
    
    import java.io.*;
    import java.util.Random;
    import java.util.Scanner;
    
    public class Main {
        int number1;
        int number2;
        char mark;
        String answer;
    
        public static void produce(Main[] calation) {
            int convert = 0;
            Random a = new Random();
            char[] ch = { '+', '-', '*', '/' }; // 字符数组
            Random b = new Random();
            for (int i = 0; i < calation.length; i++) {
                calation[i].number1 = a.nextInt(100)+1;
                calation[i].number2 = a.nextInt(100)+1;
                int node = b.nextInt(4);
                calation[i].mark = ch[node];
                switch (calation[i].mark) {
                case '+':
                    convert = calation[i].number1 + calation[i].number2;break;
                case '-':
                    convert = calation[i].number1 - calation[i].number2;break;
                case '*':
                    convert = calation[i].number1 * calation[i].number2;break;
                case '/':
                    convert = calation[i].number1 / calation[i].number2;break;
                }
                calation[i].answer = Integer.toString(convert);
            }
        }
    
        public static void storage(Main[] calation) {
            FileOutputStream fos = null;
            PrintStream ps = null;
            try {
                fos = new FileOutputStream("Test.txt", true);
                ps = new PrintStream(fos);
                for (int i = 0; i < calation.length; i++) {
                    ps.print(calation[i].number1);
                    ps.print(calation[i].mark);
                    ps.print(calation[i].number2);
                    ps.println('=');
                }
                fos.close();
                ps.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static String readTxtLine(String txtPath, int lineNo) {
    
            String line = "";
            String encoding = "GBK";
            try {
                File txtFile = new File(txtPath);
                InputStream in = new FileInputStream(txtFile);
                InputStreamReader read = new InputStreamReader(in, encoding);
                BufferedReader reader = new BufferedReader(read);
                int i = 0;
                while (i < lineNo) {
                    line = reader.readLine();
                    i++;
                }
                reader.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            return line;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String txtPath = "D:/java/Better/Test.txt";
            int trueanswer = 0, falseanswer = 0, account = 0;
            int lineNo = 0;
            Main[] a = new Main[100];
            for (int i = 0; i < a.length; i++) {
                a[i]=new Main();
            }
            String result;
            produce(a);
            storage(a);
            for (int i = 0; i < a.length; i++) {
                lineNo = i + 1;
                System.out.println(readTxtLine(txtPath, lineNo));
                result = in.nextLine();
                if (result.equals("*"))
                    break;
                else if (result.equals(a[i].answer))
                    trueanswer++;
                else
                    falseanswer++;
            }
            account = trueanswer + falseanswer;
            System.out.println("您一共做了" + account + "道题");
            System.out.println("答对" + trueanswer + "道题");
            System.out.println("答错" + falseanswer + "道题");
            System.out.println("题目及答案如下");
            for (int i = 0; i <account; i++) {
                System.out.print(a[i].number1);
                System.out.print(a[i].mark);
                System.out.print(a[i].number2);
                System.out.print("=");
                System.out.println(a[i].answer);
            }
        }
    }

    我把之前的版本分模块后,每个具体的功能写进了方法里,并且把每个方法进行测试,直至单独能够运行,最后拼接成整个大程序。

  • 相关阅读:
    每日优鲜三面:在Spring Cloud实战中,如何用服务链路追踪Sleuth?
    一文就能看懂的Nginx操作详解,你还在查漏补缺吗!
    火花思维三面:说说Redis分布式锁是如何实现的!
    【秋招必备】Dubbo面试题(2021最新版)
    【秋招必备】Elasticsearch面试题(2021最新版)
    熬了一通宵!你竟然都没有弄懂陌陌面试官问的Java虚拟机内存?
    react-native-vector-icons 使用记录
    git
    在iOS项目中嵌入RN代码
    UITabBar 图标上下跳动
  • 原文地址:https://www.cnblogs.com/quxiangjia/p/9972457.html
Copyright © 2011-2022 走看看