需要一些帮助来弄清楚为什么我的代码的最后一部分会出错。谁能告诉我什么是错的?我正在使用NetBeans进行一个介绍级别的编程类,并且无法弄清楚为什么这是错误的。
我收到以下错误:
找不到符号符号:变量rat04位置:类Rational
package rational;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Rational {
// the numer and denom fields represent a fraction
// CLASS INVARIANTS:
// CI1: denom is not 0,
// CI2: denom is positive,
// CI3: numer and denom are in lowest terms.
private final int numer;
private final int denom;
//toString and toDouble
public String toString() {
return numer + "/" + denom;
}
public double toDouble() {
return 1.0 * numer / denom;
}
//utility method
private static int greatestCommonDivisor(int a, int b) {
int c;
while (b != 0) {
c = a % b;
a = b;
b = c;
}
return Math.abs(a);
}
private static void testGcd() {
System.out.println("");
System.out.println("Test Greatest Common Denominator");
System.out.println("");
for (int i = -21; i < 31; i += 2) {
for (int j = -17; j < 31; j += 3) {
System.out.printf("\n%5d%5d%5d", i, j, greatestCommonDivisor(i, j));
}
}
System.out.println("");
}
public Rational(int numer, int denom) throws ZeroDenomException {
// CI1. No way to fix, must throw exception.
if (denom == 0) {
throw new ZeroDenomException();
}
// CI2. Can fix.
if (denom < 0) {
numer *= -1;
denom *= -1;
}
// CI3. Can fix.
int gcd = greatestCommonDivisor(numer, denom);
if (gcd != 1) {
numer /= gcd;
denom /= gcd;
}
// all class invariants now satisfied, initialize fields:
this.numer = numer;
this.denom = denom;
}
public Rational(int book) throws ZeroDenomException {
//this(integer, 1);
//this(1, book);
this(book, 1);
}
public Rational() throws ZeroDenomException {
this(0);
}
private static void testClass() throws ZeroDenomException {
System.out.println("Rational tests.");
System.out.println("");
System.out.println("Test C-tor");
System.out.println("Expected outcome: 4/25, -4/25, -4/25," + "4/25 17/1, 0/1.");
System.out.println("");
Rational rat01 = new Rational(144, 900);
Rational rat02 = new Rational(-144, 900);
Rational rat03 = new Rational(144, -900);
Rational rat04 = new Rational(-144, -900);
Rational rat05 = new Rational(17);
Rational rat06 = new Rational();
System.out.println("rat01 = " + rat01);
System.out.println("rat02 = " + rat02);
System.out.println("rat03 = " + rat03);
System.out.println("rat04 = " + rat04);
System.out.println("rat05 = " + rat05);
System.out.println("rat06 = " + rat06);
}
public static void main(String[] args) {
System.out.println("Rational class");
System.out.println("Implemented by (My Name)");
try {
testClass();
//testGcd();
} catch (ZeroDenomException ex) {
System.out.println("Zero demon exception in testClass()! (J:H)");
}
System.out.println("");
System.out.println("Try bad input");
try {
Rational rat00 = new Rational(0, 0);
} catch (ZeroDenomException zde) {
System.out.println(" Expected Zero Denominator Exception. " + zde);
} catch (Exception e) {
System.out.println("Should not be a general Exception.");
}
System.out.println("");
System.out.println("");
System.out.println("Test toDouble. Expect 0.16");
System.out.println("rat04 to double: " + rat04.toDouble());
}
}
答案 0 :(得分:2)
您在rat04
中声明testClass
并在main
中使用它。范围不同。
您不能使用从一个范围到另一个范围的变量。
您可以在类(成员变量)中创建一个静态变量,以便在需要时可以从两种方法访问(但实际上不应该......)
public class Rational {
static Rational rat04;
...
}
或者只是不打印main
中的值,而是打印在testClass
所属的值中。