package src;
public class Test {
// 宽
private double width;
// 高
private double hight;
// 圆的半径
private double length;
private double p;
public Test() {
width = 0.0;
hight = 0.0;
length = 0.0;
p = 3.1415926;
}
public double getHight() {
return hight;
}
public void setHight(double hight) {
this.hight = hight;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getP() {
return p;
}
public void setP(double p) {
this.p = p;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getResualt() {
double c = 0.0;
// 计算矩形面积
double a = 2 * (this.getHight() * this.getWidth());
// 计算圆形面积
double b = p * this.getLength() * this.getLength();
// 矩形除去圆形的面积结果
c = a - b;
return c;
}
public static void main(String[] args) {
Test test = new Test();
try {
double width = new Double(args[0]).doubleValue();
double hight = new Double(args[1]).doubleValue();
double length = new Double(args[2]).doubleValue();
double p = new Double(args[3]).doubleValue();
test.setHight(hight);
test.setWidth(width);
test.setLength(length);
test.setP(p);
double result = test.getResualt();
System.out.println("结果是 " + result);
} catch (Exception e) {
// 派那个英文字符我就用派和P代替了
System.out.println("请正确输入数字,格式为“宽度 高度 半径 派”每个变量之间"
+ "必须也只能有一个空格必须全部填写正数");
e.printStackTrace();
}
}
}
直接在CMD中输入 java Test.class 宽度 高度 半径 派
执行就可以了
圆的直径是矩形的宽吗,我做做。
public class Main
{ public static void main(String args[])
{
JX jx=new JX("求矩形去圆后面积");
}
}
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
class JX extends Frame implements ActionListener
{
Label label;
TextField textLenth,textHeight;
TextArea 提示;
Button 确定;
JX(String s)
{
super(s);
setLayout(new FlowLayout());
textLenth=new TextField(8);
textHeight=new TextField(8);
label=new Label("请分别输入矩形的长与宽");
提示=new TextArea("这个程序的功能是将输入的长与宽的矩形"+'\n'+"面积减去宽对应直径的圆的面积;长比宽大");
确定=new Button("确定");
确定.addActionListener(this);
add(label);
add(textLenth);
add(textHeight);
add(确定);
add(提示);
setBounds(100,100,450,200);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent windowevent){System.exit(0);}});
}
public void actionPerformed(ActionEvent e)
{
double m,n,s,s1,s2;
final double p = 3.14;
m=Double.parseDouble(textLenth.getText());
n=Double.parseDouble(textHeight.getText());
s1=m*n;
s2=n*n*p/4;
s=(s1-s2);
String result=Double.toString(s);
提示.setText(result);
}
}
public class Compute {
private double width = 0;
private double height = 0;
public Compute(double width,double height){
setWidth(width);
setHeight(height);
}
public void setWidth(double width){
this.width = width;
}
public void setHeight(double height){
this.height = height;
}
//计算结果
public double result(){
return rectangle()-rotundity();
}
private double rectangle(){
return width*height;
}
private double rotundity(){
double diameter = width > height ? height:width;
double radii = diameter/2;
return Math.PI*radii*radii;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Compute c = new Compute(9,12);
System.out.println(c.result());
}
}
class S{
public static double count(double a,double b)
{
double r=a>b?b:a;
return (a*b-Math.*r*r);
}
}
程序外部输入的参数是什么,这个知道了,才可以编写的阿。
这个还是挺简单的。