四则运算
Posted zhang12345
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算相关的知识,希望对你有一定的参考价值。
import java.io.*; import java.util.Random; import java.util.Scanner; public class yunsuan { static Scanner sc=new Scanner(System.in); static int[] c=new int[100]; static int m=0; static int index1=0; static int index2=0; public static void main(String[] args)throws IOException { try { BufferedWriter out = new BufferedWriter(new FileWriter("D:/ps/Test.txt")); for(int n=0;n<100;n++) { int i=(int)(1+Math.random()*100); int j=(int)(1+Math.random()*100); int k=(int)(1+Math.random()*100); Random r=new Random(); int a=r.nextInt(4); int b=r.nextInt(4); if(a==0) { if(b==0) {out.write(i+"+"+j+"+"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i+j+k; } if(b==1) {out.write(i+"+"+j+"-"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i+j-k; } if(b==2) {out.write(i+"+"+j+"*"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i+j*k; } if(b==3) {out.write(i+"+"+j+"/"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i+j/k; } } if(a==1) { if(b==0) {out.write(i+"-"+j+"+"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i-j+k; } if(b==1) { out.write(i+"-"+j+"-"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i-j-k; } if(b==2) { out.write(i+"-"+j+"*"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i-j*k; } if(b==3) { out.write(i+"-"+j+"/"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i-j/k; } } if(a==2) { if(b==0) {out.write(i+"*"+j+"+"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i*j+k; } if(b==1) { out.write(i+"*"+j+"-"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i*j-k; } if(b==2) { out.write(i+"*"+j+"*"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i*j*k; } if(b==3) { out.write(i+"*"+j+"/"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i*j/k; } } if(a==3) { if(b==0) {out.write(i+"/"+j+"+"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i/j+k; } if(b==1) { out.write(i+"/"+j+"-"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i/j-k; } if(b==2) { out.write(i+"/"+j+"*"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i/j*k; } if(b==3) { out.write(i+"/"+j+"/"+k+"="); out.newLine(); out.write("*"); out.newLine(); c[n]=i/j/k; } } } out.close(); System.out.println("文件创建成功"); FileReader fr=new FileReader("D:/ps/Test.txt"); BufferedReader br=new BufferedReader(fr); String line=br.readLine(); while(line!=null) { if(line.length()!=1) { System.out.println(line); int x=sc.nextInt(); System.out.println(c[m]); if(x==c[m]) {System.out.println("答案正确!"); index1++;} else {System.out.println("答案错误!"); index2++; } System.out.println("答对:"+index1+"道"); System.out.println("答错:"+index2+"道"); m++; } line= br.readLine(); } br.close(); } catch (IOException e) { } /* File file = new File("E:01.html"); Runtime ce=Runtime.getRuntime(); System.out.println(file.getAbsolutePath()); ce.exec("cmd /c start "+file.getAbsolutePath());*/ } }
问题:不知道怎么将数据写入文件,后来通过数组的方式来写入文件。由于数组元素个数不正确,导致溢出,通过调整数组元素个数解决问题。
以上是关于四则运算的主要内容,如果未能解决你的问题,请参考以下文章