急,求大神帮我用JAvA写下下面这两道题的代码!!!!!!!!!!!
(2),申明椭圆类Ellipse,实现Area接口,计算椭圆面积
最好有程序而运行结果的截图
再次谢谢了 展开
1)for:
import javax.swing.*;
public class moren {
public static void main(String agrs[]){
int i=0,n=0,s=1;
n=Integer.parseInt(JOptionPane.showInputDialog("输入N的值:"));
if (0 == n)
s=1;
else
for (i=1;i<=n;i++)
s*=i;
JOptionPane.showMessageDialog(null,+n+" 的阶乘为:"+s,"阶乘计算器",JOptionPane.PLAIN_MESSAGE);
}
}
2)while:
import javax.swing.*;
public class moren {
public static void main(String agrs[]){
int i=1,n=0,s=1;
n=Integer.parseInt(JOptionPane.showInputDialog("输入N的值:"));
if (0 == n)
s=1;
else
while(i<=n)
{
s*=i;
i++;
}
JOptionPane.showMessageDialog(null,+n+" 的阶乘为:"+s,"阶乘计算器",JOptionPane.PLAIN_MESSAGE);
}
}
3)do-while:
import javax.swing.*;
public class moren {
public static void main(String agrs[]){
int i=1,n=0,s=1;
n=Integer.parseInt(JOptionPane.showInputDialog("输入N的值:"));
if (0 == n)
s=1;
else
do{
s*=i;
i++;
}while(i<=n);
JOptionPane.showMessageDialog(null,+n+" 的阶乘为:"+s,"阶乘计算器",JOptionPane.PLAIN_MESSAGE);
}
}
2、import javax.swing.*;
public interface Area{ //定义接口
public abstract double getArea();
}
class Ellipse implements Area{ //定义类
double r1,r2; // 定义两个半径
public Ellipse(double a, double b){ //构造方法
r1=a; r2=b;
}
public double getArea(){
double mj=Math.PI*r1*r2;
return mj;
}
public static void main(String[] args) {
double r1=0,r2=0;
r1=Float.parseFloat(JOptionPane.showInputDialog("输入长半径a:"));
r2=Float.parseFloat(JOptionPane.showInputDialog("输入短半径b:"));
Ellipse e1=new Ellipse(r1,r2);
JOptionPane.showMessageDialog(null,"此椭圆的面积为:"+e1.getArea(),"椭圆面积计算器",JOptionPane.PLAIN_MESSAGE);
}
}