100道题的代码
完成时间:15:40
package Class_fifth; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; import java.util.Scanner; public class The_Class_Test { //随机生成100道题目 public void scanf() throws IOException { String str[]=new String[100]; String answer[]= new String[100]; int n = 0; int x,y,m,mm; Random random = new Random(System.currentTimeMillis()); String ch[] = new String[] {"-","+","/","*"}; for(int i = 1;i<=100;i++) { x = random.nextInt(100)+1; y = random.nextInt(100)+1; m = random.nextInt(20); mm = random.nextInt(4); if(ch[mm]=="*"){ str[n]= x+"*"+y+"="; answer[n]=x*y+"";} if(ch[mm]=="/"){ str[n]= x*m+ch[mm]+x+"="; answer[n]=m+""; } if(ch[mm]=="+"){ str[n] = x+"+"+y+"="; answer[n]=x+y+""; } if(ch[mm]=="-") { if(x>y) str[n]= x+"-"+y+"="; else str[n]= y+"-"+x+"="; answer[n]=Math.abs(x-y)+""; } n++; } //将题目输入test.txt文本中 FileWriter fw = null; try { //创建字符输出流 fw = new FileWriter("test.txt"); for(int i = 0;i<100;i++) fw.write(str[i]+"\r\n"); } catch (IOException ioe) { ioe.printStackTrace(); } finally { //使用finally块来关闭文件输出流 if (fw != null) { fw.close(); } } //将答案添加到answer.text文本中 FileWriter fwas = null; try { //创建字符输出流 fwas = new FileWriter("answer.txt"); for(int i = 0;i<100;i++) fwas.write(answer[i]+"\r\n"); } catch (IOException ioe) { ioe.printStackTrace(); } finally { //使用finally块来关闭文件输出流 if (fwas != null) { fwas.close(); } } } public void print() throws IOException { //输出题目进入控制台 String str[]= new String[100]; int n =0; File a=new File("test.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){ str[n]=line; n++; } bufr.close(); } c.close(); b.close(); //把答案放进字符串数组中 String answer[]= new String[100]; n =0; File as=new File("answer.txt"); FileInputStream bs = new FileInputStream(as); InputStreamReader cs=new InputStreamReader(bs,"UTF-8"); { BufferedReader bufrs =new BufferedReader(cs); String lines = null; while((lines = bufrs.readLine())!=null){ answer[n]=lines; n++; } bufrs.close(); } cs.close(); bs.close(); //输出题目,并设置输出的格式 Scanner cin = new Scanner(System.in); int score =0; for(int i=0;i<100;i++) { String Answer; System.out.println(str[i]); Answer = cin.nextLine(); if(Answer.equals("*")) { System.out.println("您一共做了"+i+"道题"); System.out.println("您做错"+(i-score)+"题"); System.out.println("您做对"+score+"题"); System.exit(0); } else { if(Answer.equals(answer[i])) score++; } } } public static void main(String[] args) throws IOException { The_Class_Test a = new The_Class_Test(); a.scanf(); a.print(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~