IDEA中main方法如何输入参数并运行
在我学习java的过程中,遇到了如何在main中输入参数并且运行。 我在这里引用抛出异常的代码来解释如何使用:
public static void main(String[] args) { int[] arr = { 1,2,3}; int[] arr1 = { -1,0,3}; try{ // 缺少命令行参数 int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); int result = ecm(i,j); System.out.println(result); }catch(NumberFormatException e){ System.out.println("数据类型不一致"); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("缺少命令行参数"); }catch(ArithmeticException e){ System.out.println("除0"); }catch(EcDef e){ System.out.println(e.getMessage()); } }
代码中引用的ecm(i,j)方法:
public static int ecm(int i, int j) throws EcDef{ // 若在主类中不创建对象进行调用ecm方法,那就将该方法设置为静态的 if(i < 0 || j < 0){ throw new EcDef("分子或分母为负数了!"); } return i / j; }
自己定义的抛出异常就不再演示,可以自己写,也是考验自己。 首先先 先进入Configurations,然后 在argument中输入参数值,然后在点击Apply,最后点击OK就回到程序页面,最后点击运行就可以,就能看到输出结果了。
下一篇:
微信开发者工具调试大法