首选,你的程序没有问题,代码是对的;其次,你截图里的那不是编译错误,是运行报错;最后来解决你的问题:
看截图你用的eclipse写的,运行的时候报错是因为你的main方法没有传递参数进去,用eclipse调试带参数的方式main方法操作如下:
在你有main方法的类代码上右键单击,让鼠标悬停在Debug As上,右侧箭头选择如下
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/83025aafa40f4bfbfc69499d0a4f78f0f63618b9?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
点击后在弹出的框里默认会选择好你的项目,右侧是对应配置项,选择Arguments选项卡如下:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/5d6034a85edf8db17bd5acad0023dd54574e7404?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
填入你要写的参数,如1和2,但是注意形式,中间用空格隔开,如图:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/d1160924ab18972b26d7d003efcd7b899f510a0e?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
然后点击右下角的运行,就能看到你要的结果了。
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/9213b07eca80653875b777da9edda144ac34829e?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)