📜  如何在Java中查找运行时提供的参数数量?

📅  最后修改于: 2022-05-13 01:54:36.082000             🧑  作者: Mango

如何在Java中查找运行时提供的参数数量?

Java命令行参数是在程序运行时传递的参数。即为了使程序动态化,可能存在我们在运行时传递参数的情况。通常,通过命令行参数,它们会被传递,并且有多种方法可以找到提供的参数数量并获取每个参数的值。

每个Java程序都通过 main 方法启动,该方法是静态的,这意味着 main 方法不需要实例化。我们可以直接打电话。因此,调用了一个Java文件名,第一个开始的方法是 main() 方法,它有一个输入参数,它是 String[](字符串数组数据类型)

句法 :

Java
// Java code with main method. 
// It has an input argument
// with String[] datatype
  
import java.io.*;
  
class GFG {
    public static void main (String[] args) {
          
    }
}


Java
// Java program to count the number of
// command line arguments at runtime
  
public class GFG {
  
   public static void main(String[] arguments) {
  
    String wordToFind = "GFG.";
  
    System.out.println("Arguments passed at runtime. " + 
    "We can get that by using args.length and  it is " + 
                            arguments.length + " here ");
  
    for(int i = 0; i < arguments.length; i++) {    
        if (arguments[i].equalsIgnoreCase(wordToFind)) {
  
             System.out.println("Provided word "+
                          wordToFind + " is present " +  
                         "and it is at location " + (i+1));
        }
   }
 }
}


Java
// Java program to count
// the number of arguments 
// using args.length
  
// main() method is a static method which
// gets called first when java program runs
public class GFG {
  
    public static void main(String[] arguments) {
        System.out.println("Arguments passed at runtime. "+
             "We can get that by using args.length and it is = " +
                       arguments.length  + " here ");
          
        for(int i = 0; i < arguments.length; i++) {
            System.out.println("Argument " + i + " = " 
                               + arguments[i]);
        }
    }
}


当Java代码运行时,我们可以在运行时传递参数作为

或在任何喜欢的IDE(如 Eclipse )中,我们也可以在运行时传递参数,如截图所示

示例 1:

Java

// Java program to count the number of
// command line arguments at runtime
  
public class GFG {
  
   public static void main(String[] arguments) {
  
    String wordToFind = "GFG.";
  
    System.out.println("Arguments passed at runtime. " + 
    "We can get that by using args.length and  it is " + 
                            arguments.length + " here ");
  
    for(int i = 0; i < arguments.length; i++) {    
        if (arguments[i].equalsIgnoreCase(wordToFind)) {
  
             System.out.println("Provided word "+
                          wordToFind + " is present " +  
                         "and it is at location " + (i+1));
        }
   }
 }
}

Eclipse中,我们可以以上述方式提供参数:

屏幕 1

屏幕 2

在Java应用程序下 –> 单击参数。在程序参数下,提供以下屏幕中指定的所需参数

运行代码时,我们可以看到下面的输出

示例 2:查看命令行参数中是否提供了“GFG”

Java

// Java program to count
// the number of arguments 
// using args.length
  
// main() method is a static method which
// gets called first when java program runs
public class GFG {
  
    public static void main(String[] arguments) {
        System.out.println("Arguments passed at runtime. "+
             "We can get that by using args.length and it is = " +
                       arguments.length  + " here ");
          
        for(int i = 0; i < arguments.length; i++) {
            System.out.println("Argument " + i + " = " 
                               + arguments[i]);
        }
    }
}

使用命令提示符,我们可以找到如下图所示的参数:

我们可以使用 cmd 提示符和命令行运行Java程序来查找参数的数量是: