测试代码:

打印一个矩形,要求输入两个参数长宽

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里的框就是输入参数的地方

Java args参数输入(eclipse)

输入参数5 6运行

注意:这里输入的参数是字符串类型的,对应于不同的情况应作一些类型转化

结果:

Java args参数输入(eclipse)

相关文章:

  • 2021-06-09
  • 2022-01-07
  • 2021-07-12
  • 2022-03-08
  • 2022-01-16
猜你喜欢
  • 2022-12-23
  • 2021-09-28
  • 2021-09-26
  • 2022-12-23
  • 2021-12-04
相关资源
相似解决方案