我应该创建一个文件(完成,它称为“ factoriales.txt”)并在其中打印10的值! (即3628800),问题是,我似乎无法将值写入文件中.我已经知道怎么写文本了,但是这行不通…请帮忙!
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class D09T9e2
{
public static void main(String[] args)
{
try
{
File fac = new File("factoriales.txt");
if (!fac.exists())
{
fac.createNewFile();
}
System.out.println("The file has been created.");
int r = 1;
for (int i = 1; i<=10; i--)
{
r = r * i;
}
FileWriter write = new FileWriter(fac);
write.write(""+r);
write.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
感谢大家在这里,这是我解决的方法,希望您喜欢这种简单的方法:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class D09T9e2
{
public static void main(String[] args)
{
try
{
File fac = new File("factoriales.txt");
if (!fac.exists())
{
fac.createNewFile();
}
System.out.println("
----------------------------------");
System.out.println("The file has been created.");
System.out.println("------------------------------------");
int r = 1;
FileWriter wr = new FileWriter(fac);
for (int i = 1; i<=10; i++)
{
r = r * i;
wr.write(r+System.getProperty( "line.separator" ));
}
wr.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}