首页 > 其他分享 >算数题构造存入CSV

算数题构造存入CSV

时间:2023-10-31 19:11:21浏览次数:26  
标签:return int BinaryOperation 存入 Operator 算数 operand CSV public

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;
    }
}

运算结果如下

 

 

标签:return,int,BinaryOperation,存入,Operator,算数,operand,CSV,public
From: https://www.cnblogs.com/wllovelmbforever/p/17801037.html

相关文章

  • Pandas数据导入和导出:CSV、Excel、MySQL、JSON
    导入MySQL查询结果:read_sqlimportpandascon="mysql+pymysql://user:[email protected]/test"sql="SELECT*FROM`student`WHEREid=2"#sql查询df1=pandas.read_sql(sql=sql,con=con)print(df1)导入MySQL整张表:read_sql_table#整张表df2=pandas.rea......
  • octave读取paraview导出的.csv数据失败
    paraview导出数据,再使用octave进行处理,一直报错:octave:X11DISPLAYenvironmentvariablenotsetoctave:disablingGUIfeatureserror:load:failedtoreadmatrixfromfile'uz_030.csv'error:calledfrompostproc-wake_velocity.matline8column5先看一......
  • mysql 导入csv 文件
    1.获取mysql配置文件路径mysqld--verbose--help|grep.cnf2.mysqld文件添加配置[mysqld]...secure-file-priv="" 3.建表createtablet_table(bank_codetext,banktext);4.导入csv文件BankCode,Bank(表头提前移除)ACEH,ACEHAGRONIAGA,Bank......
  • scrapy中的CSVFeedSpider
    目标网站:http://beijingair.sinaapp.com/ 目标文件的格式:此处以爬取一个文件内容为例: http://beijingair.sinaapp.com/data/beijing/all/20131205/csv爬取更多文件:文件中的数据格式: 1.创建项目:scrapy startprojectCSVpro2.创建爬虫后的初始化spider类:scrapy......
  • jmeter里csv 参数化 遇到乱码解决办法
    第一招以TXT打开csv文件,打开,另存为时选择utf-8 第二招修改jmeter配置文件 ......
  • 自己封装的一些算数工具方法
    工作中计算的比较多,自己封装了一些工具方法,感觉还是挺好用的。packagecom.yunmeng.iot.common.utils;importjava.math.BigDecimal;importjava.math.RoundingMode;importjava.util.List;importjava.util.Objects;importjava.util.function.Function;importjava.util......
  • 【从零学习python 】05. Python中运用算数运算符进行计算和字符串拼接
    进制现代的计算机和依赖计算机的设备里都用到二进制(即0和1)来保存和表示数据,一个二进制表示一个比特(Bit)。在二进制的基础上,计算机还支持八进制和十六进制这两种进制。除了计算机里的进制以外,我们生活中经常用到的是十进制。Python语言支持二进制、八进制、十六进制以......
  • 【从零学习python 】05. Python中运用算数运算符进行计算和字符串拼接
    进制现代的计算机和依赖计算机的设备里都用到二进制(即0和1)来保存和表示数据,一个二进制表示一个比特(Bit)。在二进制的基础上,计算机还支持八进制和十六进制这两种进制。除了计算机里的进制以外,我们生活中经常用到的是十进制。Python语言支持二进制、八进制、十六进制以......
  • 【从零学习python 】05. Python中运用算数运算符进行计算和字符串拼接
    进制现代的计算机和依赖计算机的设备里都用到二进制(即0和1)来保存和表示数据,一个二进制表示一个比特(Bit)。在二进制的基础上,计算机还支持八进制和十六进制这两种进制。除了计算机里的进制以外,我们生活中经常用到的是十进制。Python语言支持二进制、八进制、十六进制以......
  • CSV文件的读取与写入
    写入CSV文件save_path='/home/wp/st_detection/download_code/toy.csv'withopen(save_path,'w',newline='')ascsvfile:det_writer=csv.writer(csvfile,delimiter=',',quotechar='|',quoting=csv.QUOTE_MIN......