我如何通过调用具有命令行参数的函数来传递参数 [英] How I must pass parameters by calling a function which is having the command line arguments

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

问题描述

A类{

static void solve(){

public static void main(String [] args){

System.out .println(name+ args [0]);

System.out.println(city+ args [2]);

}

}

B级{

public static void main(String [] args){

System.out.println(我怎样才能打电话给上课..?我必须使用什么类型的参数。?);

A.solve();

}

}



我尝试了什么:



i不知道是什么给出参数...因为我是一个新的lerner你能用什么格式说我必须给出参数以及如何...?

class A{
static void solve(){
public static void main(String[] args){
System.out.println("name"+args[0]);
System.out.println("city"+args[2]);
}
}
class B{
public static void main(String[]args){
System.out.println("how can i call above class..? what type of parameters i must use.?");
A.solve();
}
}

What I have tried:

i dont know what to give parameters... because i am a new lerner can u say in what format i must give parameters and how..?

推荐答案

class A{
    static void solve(){
        public static void main(String[] args){
        System.out.println("name"+args[0]);
        System.out.println("city"+args[2]);
    }
}



您必须在命令行中使用两个参数调用上述程序,例如:


You must call the above program with two arguments on the command line, something like:

java A argZero Mussoorie



但是为了正确使用参数,你应该检查 args的长度数组,因为尝试访问不存在的元素会引发异常。



请尝试以下代码:


But in order to use the arguments properly you should check the length of the args array, as trying to access an element that does not exist will throw an exception.

Try the following code:

int index;
System.out.format("Application arguments:%n");
for (index = 0; index < args.length; index++) {
    System.out.format("\t%s%n", arg[index]);
}



或者


or perhaps

if (args.length > 0) {
    System.out.format("Application arguments:%n");
        for (String argName : args) {
            System.out.format("\t%s%n", argName);
        }
    System.out.format("%n");
}



命令如


with commands like

java A one two three
java A "one two three"

... etc.


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

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