随机产生四则运算,导入导出文件
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.File;
import java.util.Random;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
public class Math
{
public static void math(int[] num3) throws FileNotFoundException
{
File ps = new File("G:\\JAVA\\新建文件夹\\MATH2\\sz.txt");
FileOutputStream fos = new FileOutputStream(ps);
PrintStream p = new PrintStream(fos);
int[] num1=new int[100];
int[] num2=new int[100];
int symbol;
for(int i=0;i<100;i++)
{
num1[i]=new Random().nextInt(100);/*产生随机数*/
num2[i]=new Random().nextInt(100);
}
for(int i=0;i<100;i++)/*完成随机产生加减乘除*/
{
symbol=new Random().nextInt(4);
if(symbol==0) {
p.println(num1[i]+"+"+num2[i]+"=");
p.println("********************");
num3[i]=num1[i]+num2[i];
}
else if(symbol==1) {
p.println(num1[i]+"-"+num2[i]+"=");
p.println("********************");
num3[i]=num1[i]-num2[i];
}
else if(symbol==2)
{while(num1[i]==0)
num1[i]=new Random().nextInt(100);
while(num2[i]==0)
num2[i]=new Random().nextInt(100);
p.println(num1[i]+"*"+num2[i]+"=");
p.println("********************");
num3[i]=num1[i]*num2[i];}
else if(symbol==3)
{while(num1[i]==0)
num1[i]=new Random().nextInt(100);
while(num2[i]==0)
num2[i]=new Random().nextInt(100);
while(num1[i]%num2[i]!=0){
num1[i]=new Random().nextInt(100);
num2[i]=new Random().nextInt(100);
while(num1[i]==0)
num1[i]=new Random().nextInt(100);
while(num2[i]==0)
num2[i]=new Random().nextInt(100);
}
p.println(num1[i]+"/"+num2[i]+"=");
p.println("********************");
num3[i]=num1[i]/num2[i];}
}
}
public static void main(String args[]) throws IOException
{
int[] num4=new int[100];
math(num4);
read(num4);
}
private static void read(int[] num3) throws IOException {
Scanner sc=new Scanner(System.in);
int q;
int s=0;
int i=0;
int f=0;
File file=new File("G:\\JAVA\\新建文件夹\\MATH2\\sz.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
line=bufferedReader.readLine();
System.out.println(line);
try {
q=sc.nextInt();
if(q==num3[i]) {
s++;
i++;
System.out.println("正确");
}else {
System.out.println("答案错误,正确答案是:"+num3[i]);
f++;
i++;
}
}catch(Exception e) {
System.out.println("得分为"+s+"分"+",做错题数为"+f+"道");
}
}
System.out.println("得分为"+s+"分"+",做错题数为"+f+"道");
bufferedReader.close();
}
}
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.File;
import java.util.Random;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
public class Math
{
public static void math(int[] num3) throws FileNotFoundException
{
File ps = new File("G:\\JAVA\\新建文件夹\\MATH2\\sz.txt");
FileOutputStream fos = new FileOutputStream(ps);
PrintStream p = new PrintStream(fos);
int[] num1=new int[100];
int[] num2=new int[100];
int symbol;
for(int i=0;i<100;i++)
{
num1[i]=new Random().nextInt(100);/*产生随机数*/
num2[i]=new Random().nextInt(100);
}
for(int i=0;i<100;i++)/*完成随机产生加减乘除*/
{
symbol=new Random().nextInt(4);
if(symbol==0) {
p.println(num1[i]+"+"+num2[i]+"=");
p.println("********************");
num3[i]=num1[i]+num2[i];
}
else if(symbol==1) {
p.println(num1[i]+"-"+num2[i]+"=");
p.println("********************");
num3[i]=num1[i]-num2[i];
}
else if(symbol==2)
{while(num1[i]==0)
num1[i]=new Random().nextInt(100);
while(num2[i]==0)
num2[i]=new Random().nextInt(100);
p.println(num1[i]+"*"+num2[i]+"=");
p.println("********************");
num3[i]=num1[i]*num2[i];}
else if(symbol==3)
{while(num1[i]==0)
num1[i]=new Random().nextInt(100);
while(num2[i]==0)
num2[i]=new Random().nextInt(100);
while(num1[i]%num2[i]!=0){
num1[i]=new Random().nextInt(100);
num2[i]=new Random().nextInt(100);
while(num1[i]==0)
num1[i]=new Random().nextInt(100);
while(num2[i]==0)
num2[i]=new Random().nextInt(100);
}
p.println(num1[i]+"/"+num2[i]+"=");
p.println("********************");
num3[i]=num1[i]/num2[i];}
}
}
public static void main(String args[]) throws IOException
{
int[] num4=new int[100];
math(num4);
read(num4);
}
private static void read(int[] num3) throws IOException {
Scanner sc=new Scanner(System.in);
int q;
int s=0;
int i=0;
int f=0;
File file=new File("G:\\JAVA\\新建文件夹\\MATH2\\sz.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
line=bufferedReader.readLine();
System.out.println(line);
try {
q=sc.nextInt();
if(q==num3[i]) {
s++;
i++;
System.out.println("正确");
}else {
System.out.println("答案错误,正确答案是:"+num3[i]);
f++;
i++;
}
}catch(Exception e) {
System.out.println("得分为"+s+"分"+",做错题数为"+f+"道");
}
}
System.out.println("得分为"+s+"分"+",做错题数为"+f+"道");
bufferedReader.close();
}
}
两个半小时完成,一路懵逼,懵逼树上懵逼果,懵逼树下你和我,在舍友的帮助下完成了。