-1

クラスの Java プログラムに問題があります。プログラムが取る方程式の係数の行を含む出力ファイルのパスと名前をユーザーに要求するプログラムを作成し、二次式を使用して解を計算する必要があります。これまでのところ、出力ファイルを除いてすべてが正しいと思います。3 行の係数を含む入力ファイルがあるとします。プログラムはコンソール ストリームに解を表示しますが、出力ファイルには 1 行の解しか表示しません。

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();
4

1 に答える 1