编译器错误,我不明白
compiler errors, I do not underdstand
我想让它在我 运行 ComplexTest.class 的地方工作,然后在那个 class 它 运行s Complex.class。我是 java 的新手,我不知道出了什么问题。不确定为什么编译器希望看到 .class 和它认为应该出现的分号。
主要class
public class ComplexTest
{
//private final double re; // the real part
//private final double im; // the imaginary part
public static void main(String[] paramArrayOfString)
{
CreateObjs();
PrintHeader1();
PrintHeader2();
// invoke and Initialize a Complex object
Complex Comp = new Complex(); // Invokes Complex constructor (pg 315)
Comp.JunctionBox(CompA, CompB);
// multiply.printQuestionResult();
}
public static void CreateObjs()
{
Complex CompA = new Complex(9.5D, 7.7D);
Complex CompB = new Complex(1.2D, 3.1D);
}
public static void PrintHeader1()
{
System.out.printf(" A complex number in the \n form (x, y) is equal to \n x + yi, where i is \n square root of -1.\n");
}
public static void PrintHeader2()
{
System.out.printf("\n *-Complex numbers calculations-*");
}
}
第 2 class
/******************************************************************************
* Data type for complex numbers.
*
* The data type is "imagmutable" so once you create and initialize
* a Complex object, you cannot change it. The "final" keyword
* when declaring re and imag enforces this rule, making it a
* compile-timage error to change the .re or .imag fields after
* they've been initialized.
*
* % java Complex
* a = 5.0 + 6.0i
* b = -3.0 + 4.0i
* b + a = 2.0 + 10.0i
* a - b = 8.0 + 2.0i
* a * b = -39.0 + 2.0i
* a / b = 0.36 - 1.52i
******************************************************************************/
public class Complex {
// Constants (final)
private final double re; // the real part
private final double imag; // the imaginaryinary part
// Variables
public double product;
// create a new object with the given real and imaginaryinary parts
public Complex(double real, double imaginary) {
re = real;
imag = imaginary;
}
// return a string representation of the invoking Complex object
public String toString() {
if (imag == 0) return "<" + re + ">";
if (re == 0) return "<" + imag + ">";
if (imag < 0) return "<" + re + " - " + (-imag) + ">";
return "<" + re + ", " + imag + ">";// + "i";
}
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imaginary = a.imag + b.imag;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imaginary = a.imag - b.imag;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is (this * b)
public Complex timages(Complex b) {
Complex a = this;
double real = a.re * b.re - a.imag * b.imag;
double imaginary = a.re * b.imag + a.imag * b.re;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = re*re + imag*imag;
return new Complex(re / scale, -imag / scale);
}
// return the real or imaginaryinary part
public double re() { return re; }
public double imag() { return imag; }
// return a / b
public Complex divides(Complex b) {
Complex a = this;
return a.timages(b.reciprocal());
}
// sample client for testing
public static void main(String[] args) {
Complex a = new Complex(9.5, 7.7);
Complex b = new Complex(1.2, 3.1);
System.out.printf("a = %s\n", a);
System.out.println("b = " + b);
System.out.println("a + b = " + a.plus(b));
System.out.println("a - b = " + a.minus(b));
System.out.println("a * b = " + a.timages(b));
System.out.println("a / b = " + a.divides(b));
}
}
Compiler/Syntax 错误:
ComplexTest.java:15: error: constructor Complex in class Complex cannot be applied to given types;
Complex Comp = new Complex(); // Invokes Complex constructor (pg 315)
^
required: double,double
found: no arguments
reason: actual and formal argument lists differ in length
ComplexTest.java:16: error: cannot find symbol
Comp.JunctionBox(CompA, CompB);
^
symbol: variable CompA
location: class ComplexTest
ComplexTest.java:16: error: cannot find symbol
Comp.JunctionBox(CompA, CompB);
^
symbol: variable CompB
location: class ComplexTest
3 errors
EDIT1:修复了垃圾class,更新了错误代码块。我知道垃圾 class 是个问题。
EDIT2:我需要更多帮助,我在尝试修复已有的错误时犯了更多错误。
你有一个 "junk" class 声明把文件弄乱了。
public class JunctionBox() {
}
... 不是一个有效的 class 声明开头(括号不应该在那里),你应该只有一个 public class 声明 - 与class 命名为文件 - 在每个 Java 文件中。
删除 class 声明将使文件编译正确。
问题是由于连接了双精度和字符串,您必须首先以各种可能的方式将它们转换为字符串
例如通过以下方式
改变
if (imag == 0) return "<" + re + ">"
到
if (imag == 0) return "<" + String.valueOf(re) + ">"
一些错误是:
1) 在 ComplexTest 的主要方法中调用您尚未定义的空 Constructor Complex()。
2) 在下一行代码中您使用了 CompA 和 CompB 对象,但您没有定义它们。
3) 在第二个文件中,您首先声明了 JunctionBox Class,但是您将一个主要方法方法放在同一文件的辅助 class 中。
先更正上述错误和您质疑的更新。
我想让它在我 运行 ComplexTest.class 的地方工作,然后在那个 class 它 运行s Complex.class。我是 java 的新手,我不知道出了什么问题。不确定为什么编译器希望看到 .class 和它认为应该出现的分号。
主要class
public class ComplexTest
{
//private final double re; // the real part
//private final double im; // the imaginary part
public static void main(String[] paramArrayOfString)
{
CreateObjs();
PrintHeader1();
PrintHeader2();
// invoke and Initialize a Complex object
Complex Comp = new Complex(); // Invokes Complex constructor (pg 315)
Comp.JunctionBox(CompA, CompB);
// multiply.printQuestionResult();
}
public static void CreateObjs()
{
Complex CompA = new Complex(9.5D, 7.7D);
Complex CompB = new Complex(1.2D, 3.1D);
}
public static void PrintHeader1()
{
System.out.printf(" A complex number in the \n form (x, y) is equal to \n x + yi, where i is \n square root of -1.\n");
}
public static void PrintHeader2()
{
System.out.printf("\n *-Complex numbers calculations-*");
}
}
第 2 class
/******************************************************************************
* Data type for complex numbers.
*
* The data type is "imagmutable" so once you create and initialize
* a Complex object, you cannot change it. The "final" keyword
* when declaring re and imag enforces this rule, making it a
* compile-timage error to change the .re or .imag fields after
* they've been initialized.
*
* % java Complex
* a = 5.0 + 6.0i
* b = -3.0 + 4.0i
* b + a = 2.0 + 10.0i
* a - b = 8.0 + 2.0i
* a * b = -39.0 + 2.0i
* a / b = 0.36 - 1.52i
******************************************************************************/
public class Complex {
// Constants (final)
private final double re; // the real part
private final double imag; // the imaginaryinary part
// Variables
public double product;
// create a new object with the given real and imaginaryinary parts
public Complex(double real, double imaginary) {
re = real;
imag = imaginary;
}
// return a string representation of the invoking Complex object
public String toString() {
if (imag == 0) return "<" + re + ">";
if (re == 0) return "<" + imag + ">";
if (imag < 0) return "<" + re + " - " + (-imag) + ">";
return "<" + re + ", " + imag + ">";// + "i";
}
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imaginary = a.imag + b.imag;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imaginary = a.imag - b.imag;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is (this * b)
public Complex timages(Complex b) {
Complex a = this;
double real = a.re * b.re - a.imag * b.imag;
double imaginary = a.re * b.imag + a.imag * b.re;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = re*re + imag*imag;
return new Complex(re / scale, -imag / scale);
}
// return the real or imaginaryinary part
public double re() { return re; }
public double imag() { return imag; }
// return a / b
public Complex divides(Complex b) {
Complex a = this;
return a.timages(b.reciprocal());
}
// sample client for testing
public static void main(String[] args) {
Complex a = new Complex(9.5, 7.7);
Complex b = new Complex(1.2, 3.1);
System.out.printf("a = %s\n", a);
System.out.println("b = " + b);
System.out.println("a + b = " + a.plus(b));
System.out.println("a - b = " + a.minus(b));
System.out.println("a * b = " + a.timages(b));
System.out.println("a / b = " + a.divides(b));
}
}
Compiler/Syntax 错误:
ComplexTest.java:15: error: constructor Complex in class Complex cannot be applied to given types;
Complex Comp = new Complex(); // Invokes Complex constructor (pg 315)
^
required: double,double
found: no arguments
reason: actual and formal argument lists differ in length
ComplexTest.java:16: error: cannot find symbol
Comp.JunctionBox(CompA, CompB);
^
symbol: variable CompA
location: class ComplexTest
ComplexTest.java:16: error: cannot find symbol
Comp.JunctionBox(CompA, CompB);
^
symbol: variable CompB
location: class ComplexTest
3 errors
EDIT1:修复了垃圾class,更新了错误代码块。我知道垃圾 class 是个问题。
EDIT2:我需要更多帮助,我在尝试修复已有的错误时犯了更多错误。
你有一个 "junk" class 声明把文件弄乱了。
public class JunctionBox() {
}
... 不是一个有效的 class 声明开头(括号不应该在那里),你应该只有一个 public class 声明 - 与class 命名为文件 - 在每个 Java 文件中。
删除 class 声明将使文件编译正确。
问题是由于连接了双精度和字符串,您必须首先以各种可能的方式将它们转换为字符串 例如通过以下方式
改变
if (imag == 0) return "<" + re + ">"
到
if (imag == 0) return "<" + String.valueOf(re) + ">"
一些错误是: 1) 在 ComplexTest 的主要方法中调用您尚未定义的空 Constructor Complex()。 2) 在下一行代码中您使用了 CompA 和 CompB 对象,但您没有定义它们。 3) 在第二个文件中,您首先声明了 JunctionBox Class,但是您将一个主要方法方法放在同一文件的辅助 class 中。 先更正上述错误和您质疑的更新。