如何在java中传递命令行参数 [英] how to pass Command line arguments in java

查看:481
本文介绍了如何在java中传递命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何接受两位数作为命令行参数并打印其数字之和。如果两位数字是88输出将是16.这里我的任务是不使用条件或循环语句。

How to accept two digit number as command line argument and print the sum of its digits. If two digit number is 88 output will be 16. Here my task is to dont use conditional or looping statements.

推荐答案

使用从命令行传递的第一个字符串,添加每个数字的转换整数。

Just use the first string passed from the command line adding the casted integers of each digit.

public class Test
{
    public static void main(String[] args)
    {
        int a = Integer.parseInt(args[0].substring(0, 1));
        int b = Integer.parseInt(args[0].substring(1, 2));

        System.out.printf("Sum: %d", a + b);
    }
}

这篇关于如何在java中传递命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆