运算存储
package ruanjiangouzao;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class four {
static int num;
static String []s=new String [100];
public static void main(String args[]){
//设置连接的路径
File writeFile=new File("C:\\Users\\86186\\Desktop\\tiku.csv");
Scanner sc=new Scanner(System.in);
System.out.println("请输入存储的题目个数:");
int n= sc.nextInt();
try {
//通过BufferedReader类创建一个使用默认大小输出缓冲区的缓冲字符输出流
BufferedWriter writer=new BufferedWriter(new FileWriter(writeFile));
writer.write("题目"+",标准答案");
writer.newLine();
for (int i=0;i<n;i++){
String w=creat();
if (test(w,i)){
continue; //如果重复就跳过循环,不进行存入
}
writer.write(w+","+num);
writer.newLine();
}
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
System.out.println("写入失败");
}catch (IOException e){
System.out.println("写入成功");
}
}
//创建运算式题目
public static String creat(){
int x,y,z;
Random rand=new Random();
x= rand.nextInt(100)+1;
y=rand.nextInt(100)+1;
z=rand.nextInt(50)+1;
String str="";
if (z%2==0){
num=x+y;
str=x+"+"+y;
return str;
}else {
num=x-y;
str=x+"-"+y;
return str;
}
}
//判断算式是否重复,包括反向的重复
public static boolean test(String w,int i){
String w1=new StringBuffer(w).reverse().toString();
for (int j=0;j<i;j++){
if (w.equals(s[j])&&w1.equals(s[j])){
return true;
}
}
return false;
}
}