public class Main {
public class Fraction{
public int dividend;//被除数
public int divisor;//除数
Fraction(int a, int b)
{
dividend = a/gcd(a,b);
divisor = b/gcd(a,b);
}
double toDouble()
{
return (double)dividend/(double)divisor;
}
Fraction plus(Fraction r)
{
int lcm = lcm(divisor,r.divisor);
int gcd = gcd(divisor,r.divisor);
int new_dividend = dividend * r.divisor / gcd + divisor * r.dividend / gcd;
int new_divisor = lcm;
return new Fraction(new_dividend, new_divisor);
}
Fraction multiply(Fraction r)
{
int new_dividend = dividend * r.dividend;
int new_divisor = divisor * r.divisor;
this.dividend = new_dividend/gcd(new_dividend,new_divisor);
this.divisor = new_divisor/gcd(new_dividend,new_divisor);
return new Fraction(dividend,divisor);
}
刚学java,对于类不是很熟悉,了解得不深,写出来后报错
No enclosing instance of type XXX is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main). Main.java /week01/src/week01 line 83 Java Problem
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Fraction a = new Fraction(in.nextInt(), in.nextInt());//抱歉
Fraction b = new Fraction(in.nextInt(),in.nextInt());
在网上查了一下,发现这是 因为内部类是动态的(无static关键字修饰),而main方法是静态的,不能直接调用。
修改方法就是在类的前面加一个static 修饰,这样就行了
public class Main {
public static class Fraction{
public int dividend;//被除数
public int divisor;//除数