gpt4 book ai didi

Java CSV 编写

转载 作者:行者123 更新时间:2023-12-04 20:18:29 29 4
gpt4 key购买 nike

我目前正在尝试将数据写入 excel 以生成报告。我可以将数据写入 csv 文件,但它不会按照我想要的顺序出现在 excel 中。我需要数据在每列的最佳和最差适应性下打印,而不是全部打印在平均值下。这是相关代码,我们将不胜感激:

    String [] Fitness = "Average fitness#Worst fitness #Best Fitness".split("#");
writer.writeNext(Fitness);

//takes data from average fitness and stores as an int
int aFit = myPop.individuals[25].getFitness();
//converts int to string
String aFit1 = Integer.toString(aFit);
//converts string to string array
String aFit2 [] = aFit1.split(" ");
//writes to csv
writer.writeNext(aFit2);
//String [] nextCol = "#".split("#");



int wFit = myPop.individuals[49].getFitness();
String wFit1 = Integer.toString(wFit);
String wFit2 [] = wFit1.split(" ");
writer.writeNext(wFit2);

int bFit = myPop.individuals[1].getFitness();
String bFit1 = Integer.toString(bFit);
String bFit2 [] = bFit1.split(" ");
writer.writeNext(bFit2);

enter image description here

最佳答案

我认为您应该为每行数据调用一次“writeNext”方法:

String [] Fitness = "Average fitness#Worst fitness #Best Fitness".split("#");
writer.writeNext(Fitness);

int aFit = myPop.individuals[25].getFitness();
String aFit1 = Integer.toString(aFit);

int wFit = myPop.individuals[49].getFitness();
String wFit1 = Integer.toString(wFit);

int bFit = myPop.individuals[1].getFitness();
String bFit1 = Integer.toString(bFit);

writer.writeNext(new String[]{aFit1, wFit1, bFit1});

关于Java CSV 编写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40769094/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com