
求大神告知完整代码!!!! 5
1个回答
展开全部
public class CalRect {
public static void main(String args[]){
try{
//检查参数个数是否正确
if(args.length != 2){
throw new LackOfParamsException("参数个数不对,此处要求输入长和宽两个整数参数!");
}
//检查参数是否为大于等于0的正整数
int a=0,b=0;
for (int i = 0; i < args.length; i++) {
int temp = Integer.parseInt(args[i]);
if(temp <= 0){
throw new ArithmeticException();
}
if(i==0){
a = temp;
}else {
b = temp;
}
}
//周长
int perimeter = 2*(a+b);
//面积
int square = a*b;
//图形
StringBuffer graph = new StringBuffer();
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if(i==0 || i==a-1){
graph.append("*");
}else {
if(j==0 || j==b-1){
graph.append("*");
}else {
graph.append(" ");
}
}
//换行
if(j==b-1){
graph.append("\n");
}
}
}
//打印
print("矩形周长:" + perimeter);
print("矩形面积:" + square);
print("矩形形状:\n" + graph.toString());
}catch (LackOfParamsException e){
System.err.println(e.getMessage());
}catch (NumberFormatException e){
System.err.println("请输入整数,而非其它字母或特殊符号!");
}catch (ArithmeticException e){
System.err.println("请输入大于0的正整数!");
}finally {
System.out.println("结束!");
}
}
private static void print(Object o){
System.out.println(o);
}
}
/**
* 缺少参数异常
*/
class LackOfParamsException extends Exception{
public LackOfParamsException(String errMsg){
super(errMsg);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询