日期:2014-05-20 浏览次数:21039 次
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);