1个回答
2018-11-24 · 百度知道合伙人官方认证企业
关注
展开全部
import java.util.Scanner;
interface interfaceA{
double method(int n);
}
class A implements interfaceA{
public double method(int n) {
double sum = 0;
for(int i = 0; i <= n; i++){
sum +=i;
}
return sum;
}
}
class B implements interfaceA{
public double method(int n) {
double sum = 1;
for(int i = 1; i <= n; i++){
sum = sum * i;
}
return sum;
}
}
public class exp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入N:");
int n = scanner.nextInt();
interfaceA a1 = new A();
interfaceA b1 = new B();
System.out.println("1到" + n + "的和为:" + a1.method(n));
System.out.println("1到" + n + "的积为:" + b1.method(n));
}
}
interface interfaceA{
double method(int n);
}
class A implements interfaceA{
public double method(int n) {
double sum = 0;
for(int i = 0; i <= n; i++){
sum +=i;
}
return sum;
}
}
class B implements interfaceA{
public double method(int n) {
double sum = 1;
for(int i = 1; i <= n; i++){
sum = sum * i;
}
return sum;
}
}
public class exp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入N:");
int n = scanner.nextInt();
interfaceA a1 = new A();
interfaceA b1 = new B();
System.out.println("1到" + n + "的和为:" + a1.method(n));
System.out.println("1到" + n + "的积为:" + b1.method(n));
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询