Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复我的printWriter并获得文本文件的完整输出_Java - Fatal编程技术网

Java 如何修复我的printWriter并获得文本文件的完整输出

Java 如何修复我的printWriter并获得文本文件的完整输出,java,Java,这就是我得到的: import java.io.File; import java.io.PrintWriter; public class Hw6Problem004 { public static void main(String[] args) throws Exception { { double j = 120; for (double i = 40

这就是我得到的:

    import java.io.File;
    import java.io.PrintWriter;

    public class Hw6Problem004 {

        public static void main(String[] args) throws Exception {
            {



                double j = 120;
                for (double i = 40; i >= 31; i--) {

                    //Create File object for the file 
                    File outFile = new File("temperature_table.text");

                    //Create the PrintWriter object
                    PrintWriter pw = new PrintWriter(outFile);

                    pw.println("celsius  \tFahrenheit \t|       Fahrenheit \tCelsius");
                    double cTf = (double) Math.round(celsiusToFahrenheit(i) * 100) / 100;
                    double decimal = (double) Math
                            .round(fahrenheitToCelsius(j) * 100) / 100;
                    pw.println(i + "\t\t" + cTf + "\t\t|" + "\t" + j + "\t" + "\t"
                            + decimal);
                    System.out.println("");
                    j -= 10;

                    pw.close();
                }

            }

        }

        // • Converts a Celsius value to Fahrenheit
        public static double celsiusToFahrenheit(double celsius) {

            return (9.0 / 5) * celsius + 32;

        }

        // Converts a Fahrenheit value to Celsius
        public static double fahrenheitToCelsius(double fahrenheit) {

            return (5.0 / 9) * (fahrenheit - 32);

        }
    }
我需要得到这个:

 //       celsius   Fahrenheit  |       Fahrenheit  Celsius
 //       31.0      87.8        |   30.0        -1.11
我只得到文本文件中表格的最后一行。
我需要在文本文件中显示整个输出。

您应该在循环外部打开和关闭PrintWriter:

 //  celsius      Fahrenheit    | Fahrenheit     Celsius
 //   40.0      104.0       |   120.0       48.89
 //   39.0      102.2       |   110.0       43.33
 //   38.0      100.4       |   100.0       37.78
 //   37.0      98.6        |   90.0        32.22
 //   36.0      96.8        |   80.0        26.67
 //   35.0      95.0        |   70.0        21.11
 //   34.0      93.2        |   60.0        15.56
 //   33.0      91.4        |   50.0        10.0
 //   32.0      89.6        |   40.0        4.44
 //   31.0      87.8        |   30.0        -1.11   

当前,在循环的每次迭代中,您都会覆盖输出文件。

您会为每一行创建一个新的
文件。您应该将其置于循环之外,并且只创建一次文件。否则,您将继续使用当前循环迭代覆盖文件;无法解决@Efran@user3269660我的错误。PrintWriter创建也应该在循环之外。看我的编辑。哦,我明白了:)我明白了
            File outFile = new File("temperature_table.text");
            PrintWriter pw = new PrintWriter(outFile);
            for (double i = 40; i >= 31; i--) {                   

                pw.println("celsius  \tFahrenheit \t|       Fahrenheit \tCelsius");
                double cTf = (double) Math.round(celsiusToFahrenheit(i) * 100) / 100;
                double decimal = (double) Math
                        .round(fahrenheitToCelsius(j) * 100) / 100;
                pw.println(i + "\t\t" + cTf + "\t\t|" + "\t" + j + "\t" + "\t"
                        + decimal);
                System.out.println("");
                j -= 10;
            }
            pw.close();