Java程序只输出一行代码

时间:2013-10-29 20:23:18

标签: java fileinputstream fileoutputstream

我的班级java程序出了问题。我必须创建一个程序,提示用户输出文件的路径和名称,该文件将包含我的程序将采用的方程系数,并使用二次公式计算解。到目前为止,我认为一切都是正确的,除了我的输出文件。假设我有一个包含3行系数的输入文件,我的程序将在控制台流中显示解决方案,但只会在我的输出文件中显示1行解决方案。这是我的第一个java课程,这对我来说太过分了!我在这里先向您的帮助表示感谢!

while (input.hasNext()) {


    a = input.nextInt();
    b = input.nextInt();
    c = input.nextInt();

    discriminant = Math.pow(b, 2) - 4 * a * c;
    ///There will be no solutions if discriminant<0
    if (discriminant < 0){
        System.out.println("There are no solutions.");
        output.println("There are no solutions.");
    }
    ///As with the above, if coefficent a = 0 no solutions
    else if (a == 0){
        System.out.println("There are no solutions.");
        output.println("There are no solutions.");
    }
    else if (discriminant == 0){
       solutionOne = (-b + Math.sqrt(discriminant)) / (2 * a);
       if (b < 0) {
           System.out.printf("%3.0fx^2 %3.0fx + %3.0f, has one      solution:%5.3f%n",a,b,c,solutionOne);
           output.printf("%3.0fx^2 %3.0fx + %3.0f has one solution:%5.3f%n",a,b,c,solutionOne);
        }
       else{
           System.out.printf("%3.0fx^2 %3.0fx + %3.0f has one  solution:%5.3f%n",a,b,c,solutionOne);
           output.printf("%3.0fx^2 %3.0fx + %3.0f has one solution:%5.3f%n",a,b,c,solutionOne);
       }

    }
       else if(discriminant>0){
           solutionOne=(-b + Math.sqrt(discriminant))/(2*a);
           twoSolutions=(-b - Math.sqrt(discriminant))/(2*a);

           if(b<0){
               System.out.printf("%3.0fx^2 %3.0fx + %3.0f has two solutions: %5.3f %5.3f%n",a,b,c,solutionOne,twoSolutions);
               output.printf("%3.0fx^2 %3.0fx + %3.0f has two solutions:5.3f %5.3f%n",a,b,c,solutionOne,twoSolutions);
           }

           else{

           System.out.printf("%3.0fx^2 %3.0fx + %3.0f has two solutions:%5.3f %5.3f%n",a,b,c,solutionOne,twoSolutions);
           output.printf("%3.0fx^2 %3.0fx + %3.0f has two solutions: %5.3f%5.3f%n",a,b,c,solutionOne,twoSolutions);
           }

    }

    output.close();

1 个答案:

答案 0 :(得分:5)

如果我正确读取您的括号,问题是您正在调用

output.close();

在循环的每次迭代结束时。在完成所有输出后,您需要在循环中调用外部