算数题构造存入CSV

ShiYong.java
package rjgz;


import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ShiYong {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.print("请选择参数范围-> ");
        int fanWei=scanner.nextInt();
        int fanWei2=scanner.nextInt();
        Exercise exercise=new Exercise();
        for (int i = 0; i < 50; i++) {
            BinaryOperation b=new BinaryOperation(fanWei2,fanWei);
            while (!exercise.Check(b)&&b.getValue()<0&&b.getValue()>100){
                b=new BinaryOperation(fanWei2,fanWei);
            }
            exercise.list.add(b);
        }
        exercise.showList();
        // 将数据转换为CSV格式的字符串
        StringBuilder csvData = new StringBuilder();
        for(BinaryOperation objB : exercise.list) {
            csvData.append(objB.getLeft_operand()).append(objB.getOperator())
                    .append(objB.getRight_operand()).append(objB.getOperator2())
                    .append(objB.getThird_operand()).append("=").append(",")
                    .append(objB.getValue()).append("\n");
        }
        // 写入CSV文件
        String csvFile = "data.csv";
        try (FileWriter writer = new FileWriter(csvFile)) {
            writer.write(csvData.toString());
            System.out.println("Data saved to " + csvFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
BinaryOperation.java
​package rjgz;

import java.util.Random;

public class BinaryOperation {
    private int Upper = 100;
    private int Lower = 0;
    private int Right_operand = 0;

    public int getUpper() {
        return Upper;
    }

    public void setUpper(int upper) {
        Upper = upper;
    }

    public int getLower() {
        return Lower;
    }

    public void setLower(int lower) {
        Lower = lower;
    }

    public int getRight_operand() {
        return Right_operand;
    }

    public void setRight_operand(int right_operand) {
        Right_operand = right_operand;
    }

    public int getLeft_operand() {
        return Left_operand;
    }

    public void setLeft_operand(int left_operand) {
        Left_operand = left_operand;
    }

    public int getThird_operand() {
        return Third_operand;
    }

    public void setThird_operand(int third_operand) {
        Third_operand = third_operand;
    }

    public int getValue() {
        return Value;
    }

    public void setValue(int value) {
        Value = value;
    }

    public char getOperator() {
        return Operator;
    }

    public void setOperator(char operator) {
        Operator = operator;
    }

    public char getOperator2() {
        return Operator2;
    }

    public void setOperator2(char operator2) {
        Operator2 = operator2;
    }

    private int Left_operand = 0;
    private int Third_operand = 0;
    private int Value = 100;
    private char Operator = '+';
    private char Operator2 = '+';
    public int calculate(){
        if(Operator=='+'&&Operator2=='+'){
            Value=Right_operand+Left_operand+Third_operand;
        }else if (Operator=='-'&&Operator2=='+'){
            Value=Left_operand-Right_operand+Third_operand;
        }else if(Operator=='+'&&Operator2=='-'){
            Value=Left_operand+Right_operand-Third_operand;
        }else if(Operator=='-'&&Operator2=='-'){
            Value=Left_operand-Right_operand-Third_operand;
        }
        return Value;
    }
    BinaryOperation(int upper,int lower){
        Random random = new Random();
        this.Upper=upper;
        this.Lower=lower;
        Left_operand=random.nextInt(upper - lower + 1) + lower;
        Right_operand=random.nextInt(upper - lower + 1) + lower;
        Third_operand=random.nextInt(upper - lower + 1) + lower;
        int h=random.nextInt(2);
        int h2=random.nextInt(2);
        if (h==1)Operator='+';
        else Operator='-';
        if (h2==1)Operator2='+';
        else Operator2='-';
        calculate();
    }
    @Override
    public String toString() {
        return "BinaryOperation{" +
                "Right_operand=" + Right_operand +
                ", Left_operand=" + Left_operand +
                ", Value=" + Value +
                ", Operator=" + Operator +
                ", Operator2=" + Operator2 +
                '}';
    }
    public Boolean equals2(BinaryOperation two){
        return this.toString().equals(two.toString());
    }
}
Exercise.java
​package rjgz;

import java.util.ArrayList;
import java.util.List;

public class Exercise {
    public List<BinaryOperation> list=new ArrayList<>();
    public void showList(){
        int i=0;
        for (BinaryOperation b :
                list) {
            System.out.print(b.getLeft_operand()+" "+b.getOperator()+" "+b.getRight_operand()+" "+b.getOperator2()+" "+b.getThird_operand()+" =           ");
        i++;
        if (i%5==0){System.out.println();}
        }
    }
    public Boolean Check(BinaryOperation b){
        for (BinaryOperation bb :
                list) {
            if (bb.equals2(b)) {
            return false;
            }}
        return true;
    }
}

运算结果如下

 

 

posted @ 2023-10-31 19:01  超爱彬宝同学  阅读(12)  评论(0)    收藏  举报