日期:2014-05-20  浏览次数:20835 次

接口实现错误
public interface A {

  public abstract long fact(int m);
  public abstract long intPower(int m,int n);
  public abstract boolean findFactor(int m,int n);
   
}

public class B implements <A>{//18行

int m;
int n;
public long fact(int m){
this.m = m;
int i;
double x = 1;
for(i = 1;i <= m;i++){
x *= i;
System.out.println(m+"的阶乘是:"+x);
}
}
  public long intPower(int m,int n){
  this.m = m;
  this.n = n;
  double y;
  if(n > 0){
  y = m*intPower(int m, int n-1);//36行
  }
  System.out.println(m+"的n次方是:"+y);
  }
  public boolean findFactor(int m,int n){
   
  this.m = m;
  this.n = n;
  if(m <= n && n%m==0){
  System.out.println("m是n的因子");
  }
  if(n <= m && m%n ==0){
  System.out.println("n是m的因子");
  }
  }
   
  public B(int m,int n){
 
  this.m = m;
  this.n = n;
  }
   
  public static void main (String[] args) {

B b = new B(2,4);
b.fact(2);
}
}
错误: 非法的类型开始 18行
错误: 非法的表达式开始 18行
错误: 需要 '.class' 36行
错误: 需要';' 36行
错误: 需要';' 36行

------解决方案--------------------
public class B implements A{//18行

y = m*intPower(m, n-1);//36行//这里是 执行方法,又不是定义,怎么能有int?
------解决方案--------------------
改了4,5个错误。
1.有返回类型却无返回值
2.概念模糊,逻辑也没有描述清楚。
Java code

interface A {

    public abstract long fact(int m);

    public abstract long intPower(int m, int n);

    public abstract boolean findFactor(int m, int n);
}

public class B implements A {//implement <A>是什么意思?

    int m;
    int n;

    public long fact(int m) {//阶乘
        this.m = m;
        int i;
        long x = 1;
        for (i = 1; i <= m; i++) {
            x *= i;
        }
        System.out.println(m + "的阶乘是:" + x);//放外面
        return x;
    }

    public long intPower(int m, int n) {
        this.m = m;
        this.n = n;
        long y = 0;
        if(n==0){//结束条件
            y = 1;
        }
        else if (n > 0){
            y = m * intPower(m, n - 1);//调用方法,递归
        }
        System.out.println(m + "的"+n+"次方是:" + y);
        return y;
    }

    public boolean findFactor(int m, int n) {//感觉逻辑有些问题。
        boolean flag = false;
        this.m = m;
        this.n = n;
        if (m <= n && n % m == 0) {
            System.out.println("m是n的因子");
            flag = true;
        }
        if (n < m && m % n == 0) {
            System.out.println("n是m的因子");
            flag= true;
        }
        return flag;
    }

    public B(int m, int n) {
        this.m = m;
        this.n = n;
    }

    public static void main(String[] args) {
        B b = new B(2, 4);
        b.fact(2);
        b.intPower(2,4);
    }
}

------解决方案--------------------
interface A
{
/**
* 是接口,为什么还用静态方法呢?
* @param m
* @return
*/

public long fact(int m);

public long intPower(int m, int n);

public boolean findFactor(int m, int n);

}

public class B implements A
{

int m;
int n;

public long fact(int m)
{
this.m = m;
int i;
long x = 1;
for (i = 1; i <= m; i++)
{
x *= i;

}
System.out.println(m + "的阶乘是:" + x);