:从命令行参数输入2个数为矩形的长和宽,需对输入数据进行有效性判断,若输入的数据为负数或不全,则通过异常处理功能显示错误信息;若输入的数据正确,则进行图形的面积和周长计算,并绘制图形,程序运行效果如下图所示:
package com.jgroup.test;
/**
* 功能:从命令行参数输入2个数为矩形的长和宽,
* 需对输入数据进行有效性判断,
* 若输入的数据为负数或不全,则通过异常处理功能显示错误信息;
* 若输入的数据正确,则进行图形的面积和周长计算,并绘制图形
*
* @author JGroup
*
* @date 2014-12-16
*
*/
public class Test24 {
public static void main(String[] args) {
try {
method(args);
} catch (Exception e) {
if (e instanceof ArithmeticException) {
System.out.println("捕获ArithmeticException异常");
}
System.out.println(e.getMessage());
} finally {
System.out.println("结束");
}
}
private static void method(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("缺少输入数据:矩形的长或者宽");
return;
}
int a = Integer.parseInt(args[1]);
int b = Integer.parseInt(args[0]);
if (a < 0 || b < 0) {
throw new ArithmeticException("输入数据小于0");
}
for (int j = 0; j < a; j++) {
for (int i = 0; i < b; i++) {
if (i == 0 || i == b - 1 || j == 0 || j == a - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
/**
* 功能:从命令行参数输入2个数为矩形的长和宽,
* 需对输入数据进行有效性判断,
* 若输入的数据为负数或不全,则通过异常处理功能显示错误信息;
* 若输入的数据正确,则进行图形的面积和周长计算,并绘制图形
*
* @author JGroup
*
* @date 2014-12-16
*
*/
public class Test24 {
public static void main(String[] args) {
try {
method(args);
} catch (Exception e) {
if (e instanceof ArithmeticException) {
System.out.println("捕获ArithmeticException异常");
}
System.out.println(e.getMessage());
} finally {
System.out.println("结束");
}
}
private static void method(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("缺少输入数据:矩形的长或者宽");
return;
}
int a = Integer.parseInt(args[1]);
int b = Integer.parseInt(args[0]);
if (a < 0 || b < 0) {
throw new ArithmeticException("输入数据小于0");
}
for (int j = 0; j < a; j++) {
for (int i = 0; i < b; i++) {
if (i == 0 || i == b - 1 || j == 0 || j == a - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-12-16
兄弟,你好好搜搜吧,上午你同学也在知道问这个问题来、
第2个回答 2014-12-16
你这不都写好了吗
第3个回答 2014-12-16
息;若输入的数据正确