gpt4 book ai didi

java - 在Java中写入文件: output displays differently in NotePad vs. Word/WordPad/NotePad++

转载 作者:行者123 更新时间:2023-12-02 00:45:09 25 4
gpt4 key购买 nike

下面是用于读取名为 Sales.txt 的现有文本文件的 Java 代码并将条形图输出到名为 storeReport.txt 的文本文件
每个星号代表 100 美元的销售额。

导入java.io.*;

导入java.util.*;

公共(public)类条形图

{ public static void main(String[] args) 抛出 FileNotFoundException、IOException

   {
int[] s = new int[5];
int[] b = new int[5];

FileWriter fstream = new FileWriter("storeReport.txt");
BufferedWriter out = new BufferedWriter(fstream);
Scanner scanner = new Scanner(new File("Sales.txt"));

int i = 0;

while(scanner.hasNext())
s[i++] = scanner.nextInt();
System.out.println("SALES BAR CHART\n");
out.write("SALES BAR CHART\n");

for (i=0; i<s.length; i++)
{
b[i] = s[i]/100;
out.write("store "+ (i+1) +" : ");
System.out.print("store "+ (i+1) +" : ");

for(int j = 1; j <= b[i]; j++) {
System.out.print("*");
out.write("*"); }
out.write("\n");
System.out.println();
}

out.close(); //Close the output stream

}
}

当我在 Word、WordPad 或 NotePad++ 中打开 storeReport.txt 时,输出按预期显示:

商店 1:***
商店 2 :
*****
商店 3 :********
商店 4:**
商店 5 :*********

当我在记事本中打开文件时,输出全部显示在一行上。 (我使用 Windows 7 和最新的 jGrasp 来编译和运行该程序。)销售条形图

输入文件 Sales.txt 内容如下:

1000
1200
1800
800
1900

任何人都知道为什么 NotePad 将输出显示为:

销售条形图商店 1:***商店 2:** **商店 3:****** **商店 4:**商店 5:<强>**********

最佳答案

我怀疑这是一个 newline问题。尝试更换您的

out.write("\n");

out.newLine();

关于java - 在Java中写入文件: output displays differently in NotePad vs. Word/WordPad/NotePad++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5214725/

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