如何让这段代码写入文本文件height.txt?它创建它,但它不写入它。 它也编译并说数据写入文件,但没有任何数据,当我打开该文件为什么?java编程,写入文本文件
import java.io.*;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileWriter;
public class readinguserinput {
public static String gender;
public static int motherHeight;
public static int fatherHeight;
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
try
{
FileWriter fw = new FileWriter("height.txt");
PrintWriter pw = new PrintWriter(fw);
System.out.println ("Enter gender");
gender = keyboard.next();
System.out.println ("Enter Mother Height");
motherHeight = keyboard.nextInt();
keyboard.nextLine();
while (motherHeight < 0)
{
System.out.println ("Enter Mother Height");
motherHeight = keyboard.nextInt();
}
System.out.println ("Enter father Height");
fatherHeight = keyboard.nextInt();
while (fatherHeight < 0)
{ System.out.println ("Enter Father Height");
fatherHeight = keyboard.nextInt();
}
pw.close();
}catch (IOException e){
System.out.println("file not found");
}
System.out.println("data written to the file");}}
他正在写控制台输出到控制台。如果他遵循这个建议,他将失去他的用户界面。 – EJP 2014-10-28 05:05:59
我给了面包屑,而不是一个面包,他不需要改变他们所有的人。 – CharlieS 2014-10-28 06:19:19
他不需要改变他写的任何东西。他们都清楚地用于控制台。他需要添加新的,包括从控制台读取的值。这里既没有面包,也没有面包屑。 – EJP 2014-10-28 08:49:11