-1

我是 Java 新手,在下面附加的代码中,我有 2 个类,Start.java 和 ecuație.java。ecuatie.java 计算二次方程的平方英尺,但由于某种原因,构造函数没有正确初始化这些值。你能告诉我为什么会这样吗?

package com.ecuatie;

import com.ecuatie.ecuatie;

public class Start {

  public static void main(String[] args) {
      ecuatie exemplu = new ecuatie(1.0d, 0.0d, -4.0d);

      System.out.println(exemplu.delta() + '\n');

      System.out.println(exemplu.X1() + '\n');
      System.out.println(exemplu.X2() + '\n');
  }
}


package com.ecuatie;

import java.lang.Math;

public class ecuatie {
       private double a = 0, b = 0, c = 0;

       ecuatie(double a, double b, double c) {
         this.a = a; this.b = b; this.c = c;
       }

       public double delta() {
         return (b * b) - (4 * a * c);
       }

       public double X1() {
         return (-b + Math.sqrt(delta())) / (2 * a);
       }

       public double X2() {
         return (-b - Math.sqrt(delta())) / (2 * a);
       }
}
4

1 回答 1

2

你得到它是因为它添加了字符的 ASCII 值。

'\n' 的 ASCII 值是 10。所以就像你 + exemplu.delta() 和 10。你在使用 println() 时也不需要添加 enter。

所以你只需要像这样编写你的代码。

  public static void main(String[] args) {
  ecuatie exemplu = new ecuatie(1.0d, 0.0d, -4.0d);

     System.out.println(exemplu.delta() );

     System.out.println(exemplu.X1() );
     System.out.println(exemplu.X2() );
  }
于 2019-03-10T08:03:24.337 回答