gpt4 book ai didi

java - 以表格形式显示

转载 作者:行者123 更新时间:2023-12-01 05:48:40 27 4
gpt4 key购买 nike

嗨,这是我计算术语频率的代码。

System.out.println("Please enter the required word  :");
Scanner scan = new Scanner(System.in);
String word = scan.nextLine();
String[] array = word.split(" ");
int filename = 11;
String[] fileName = new String[filename];
int a = 0;

for (a = 0; a < filename; a++) {
try {
File file = new File(
"C:\\Users\\user\\fypworkspace\\TextRenderer\\abc" + a
+ ".txt");
System.out.println("File = abc" + a + ".txt");
for (int i = 0; i < array.length; i++) {

System.out.println(array[i]);

int totalCount = 0;
int wordCount = 0;
int numDoc2 = 0;

Scanner s = new Scanner(file);
{
while (s.hasNext()) {
totalCount++;
if (s.next().equals(array[i]))
wordCount++;

}

System.out.println("Word count: " + wordCount);
System.out.println("Total count: " + totalCount);
System.out.printf("Term Frequency: %8.4f",
(double) wordCount / totalCount);

System.out.println("\n");

}
}
} catch (FileNotFoundException e) {
System.out.println("File is not found");

}

}

到目前为止代码显示

请输入所需的单词:关于

文件 = abc0.txt

关于

字数:0

总数量:1706

词频:0.0000

文件 = abc1.txt

关于

字数:0

总数量:9819

词频:0.0000

如何创建如下所示的数据表表单:

输出:

文件名单词总术语

abc0.txt 0.1 0.2 0.3

abc1.txt 0.4 0.5 0.6

最佳答案

不要使用System.out.println(String),而是使用System.out.print(String)

System.out.print(String) 将打印出数据,而不会导致以下 System.out.print(String) 在下一行开始。这将帮助您以正确的格式获得数据输出

将顶部文件名的打印语句更改为

System.out.print("abc" + a + ".txt");

并将末尾的打印语句更改为

System.out.print(" " + wordCount);
System.out.print(" " + totalCount);
System.out.printf(" %8.4f", (double) wordCount / totalCount);
System.out.println();

关于java - 以表格形式显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5255328/

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