我正在尝试将数字1-10的平方输出到名为Squares的文件中,但该OutputStream.print(i+"\t"+(i*i));
零件出现错误。印刷品带有下划线,我不明白为什么。请帮我。
这是代码:
import java.util.*;
import java.io.*;
public class Number1 {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream fos = new FileOutputStream("Squares.txt");
PrintWriter square;
try{
square = new PrintWriter(fos);
} catch (Exception e) {
System.out.print("Could not create/open file");
System.exit(0);
}
for(int i=1; i<=10; i++)
{
OutputStream.print(i+"\t"+(i*i));
}
}
}
完整的解决方案:
import java.io.*;
public class Number1 {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream fos = new FileOutputStream("Squares.txt");
PrintWriter square = null;
try{
square = new PrintWriter(fos);
}
catch (Exception e)
{
System.out.print("Could not create/open file");
System.exit(0);
}
for(int i=1; i<=10; i++)
{
square.print(i+"\t"+(i*i));
}
square.close();
}
}
OutputStream
没有print
方法,即使有,也可能不是静态方法。
使用您的PrintWriter
实例写入文件。
public static void main(String[] args) throws FileNotFoundException
{
FileOutputStream fos = new FileOutputStream("Squares.txt");
try{
PrintWriter square = new PrintWriter(fos);
for(int i=1; i<=10; i++) {
square.print(i+"\t"+(i*i));
}
square.close ();
}
catch (Exception e) {
System.out.print("Could not create/open file");
System.exit(0);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句