球状星团:用java编译一个计算长方形的周长与面积

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/14 01:24:10

import java.io.*;
class CBox
{
int width;
int height;

int perimeter(int width,int height)
{
return 2*(width+height);
}
int area(int width,int height )
{
return width*height;
}
}
public class chang
{
public static void main(String args[])throws IOException
{
int a,b;
String str;
BufferedReader buf1,buf2;

buf1=new BufferedReader(new InputStreamReader(System.in));
buf2=new BufferedReader(new InputStreamReader(System.in));

System.out.print("请输入2个数的值:");
str=buf1.readLine();
a=Integer.parseInt(str);

str=buf2.readLine();
b=Integer.parseInt(str);

System.out.println(a+" "+b);

CBox rect1=new CBox();

System.out.println("周长为:"+rect1.perimeter(a,b));
System.out.println("面积为:"+rect1.area(a,b));
}
}

我觉得把输入转换成数字可以这么写,
inta,b;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);

//FileName:DoRect.java

import java.lang.Math;
import java.io.*;

class Rect
{
private float width,height;

public Rect(float w,float h)
{
this.width=w;
this.height=h;
getPeri(w,h);
getArea(w,h);
}

private void getArea(float w,float h)
{
System.out.println("The area is: "+width*height);
}

private void getPeri(float w,float h)
{
System.out.println("The perimeter is: "+2*(width+height));
}
}

public class DoRect{
public static void main(String[] args)
{
float w=Float.valueOf(args[0]).floatValue();
float h=Float.valueOf(args[1]).floatValue();
Rect rect=new Rect(w,h);
}
}

//编译后执行 java DoRect x.x x.x
//如c:/>java DoRect 22.5 20.0
/***********结果如下:*******************
The perimeter is: 85.0
The Area is: 450.0
****************************************/