测试代码:
打印一个矩形,要求输入两个参数长宽
package pack3;
public class PrintStar {
public static void main(String args[])
{
try{
int length=0;
int width=0;
String str1=args[0];
String str2=args[1];
length=Integer.parseInt(str1);
width=Integer.parseInt(str2);
for(int i=0;i<width;i++) {
if(i==0 || i==width-1)
for(int j=0;j<length;j++)
System.out.print("*");
else
{
for(int j=0;j<length;j++)
if(j==0 || j==length-1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.print("\n");
}
}catch(NumberFormatException e) {
}catch(ArrayIndexOutOfBoundsException e) {}
}
}
输入参数页面:点击run->run configurations->Arguments中program argument里的框就是输入参数的地方
输入参数5 6运行
注意:这里输入的参数是字符串类型的,对应于不同的情况应作一些类型转化
结果: