集册 Java实例教程 从参数读取的整数值总和

从参数读取的整数值总和

欢马劈雪     最近更新时间:2020-01-02 10:19:05

477
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从参数读取的整数值总和

public class Adder {

    public static void main(String[] args) {

        int numArgs = args.length;
/** nowjava - 时  代  Java 提 供 **/

        //this program requires at least two arguments on the command line

        if (numArgs < 2) {

            System.out

                    .println("This program requires two command-line arguments.");

        } else {

            int sum = 0;


            for (int i = 0; i < numArgs; i++) {

                sum += Integer.valueOf(args[i]).intValue();

            }


            //print the sum

            System.out.println(sum);/**来 自 nowjava - 时  代  Java**/

        }

    }

}


展开阅读全文