标签:rgs gbk system nts 程序 answer 答案 题目 better
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); } } }
我把之前的版本分模块后,每个具体的功能写进了方法里,并且把每个方法进行测试,直至单独能够运行,最后拼接成整个大程序。
标签:rgs gbk system nts 程序 answer 答案 题目 better
原文地址:https://www.cnblogs.com/quxiangjia/p/9972457.html