运行Eclipse,我正在编写程序来为类添加分数,所以它可能看起来非常不专业,但是这里。基本上我的理性名字无法解决,我不确定为什么。它与范围有关吗?我已经评论了错误发生的位置。
package lab11tests;
import java.util.Scanner;
public class Tests {
private static int num1 = 0;
private static int den1 = 1;
private static int num2 = 0;
private static int den2 = 1;
static Rational r1 = new Rational(num1,den1);
static Rational r2 = new Rational(num2,den2);
static Rational r3 = new Rational();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Numerator 1: ");
num1=input.nextInt();
System.out.print("Denominator 1: ");
den1=input.nextInt();
System.out.print("Numerator 2: ");
num2 = input.nextInt();
System.out.print("Denominator 2: ");
den2 = input.nextInt();
System.out.println(r1.rational() + " + " + r2.rational() + " equals "+ r3.answer());
}
}
class Rational {
// private int firstNum;
// private int firstDen;
private int num;
private int den;
public Rational(int n, int d){
num = n;
den = d;
}
public Rational(){}
public int getNum(){
return num;
}
public int getDen(){
return den;
}
public void add(){
int commonDen = r1.getDen() * r2.getDen();`enter code here`
this.num=(r1.getNum()*r2.getDen())+(r2.getNum()*r1.getDen()); //Errors are here, all r2 and r1's in the add method "cannot be resolved"
this.den=commonDen;
}
public String rational(){
return num + "/" + den;
}
public String answer(){
return this.num + "/" + this.den;
}
}
答案 0 :(得分:0)
Rational是一个单独的类,Tests类括号在定义Rational类
之前已经结束