java命令行参数_Java命令行参数

java命令行参数

The command line argument is the argument that passed to a program during runtime. It is the way to pass argument to the main method in Java. These arguments store into the String type args parameter which is main method parameter.

命令行参数是在运行时传递给程序的参数。 这是将参数传递给Java中的main方法的方法。 这些参数存储在String类型的args参数中,这是主要方法参数。

To access these arguments, you can simply traverse the args parameter in the loop or use direct index value because args is an array of type String.

要访问这些参数,您可以简单地遍历循环中的args参数或使用直接索引值,因为args是String类型的数组。

For example, if we run a HelloWorld class that contains main method and we provide argument to it during runtime, then the syntax would be like.

例如,如果我们运行一个包含main方法的HelloWorld类,并在运行时为其提供参数,则语法将像这样。

java HelloWorld arg1 arg2 ...

We can pass any number of arguments because argument type is an array. Lets see an example.

我们可以传递任意数量的参数,因为参数类型是一个数组。 让我们来看一个例子。

Example

In this example, we created a class HelloWorld and during running the program we are providing command-line argument.

在此示例中,我们创建了一个HelloWorld类,在运行程序期间,我们提供了命令行参数。

class cmd
{
    public static void main(String[] args)
    {
        for(int i=0;i< args.length;i++)
        {
            System.out.println(args[i]);
        }
    }
}
Execute this program as java cmd 10 20 30 以java cmd 10 20 30的身份执行此程序

10 20 30

10 20 30

To terminate the program in between based on some condition or program logic, Java provides exit() that can be used to terminate the program at any point. Here we are discussing about the exit() method with the example.

为了根据某种条件或程序逻辑在两者之间终止程序,Java提供了exit() ,可用于随时终止程序。 在这里,我们通过示例讨论exit()方法。

Java System.exit()方法 (Java System.exit() Method)

In Java, exit() method is in the java.lang.System class. This method is used to take an exit or terminating from a running program. It can take either zero or non-zero value. exit(0) is used for successful termination and exit(1) or exit(-1) is used for unsuccessful termination. The exit() method does not return any value.

在Java中, exit()方法位于java.lang.System类中。 此方法用于退出或终止正在运行的程序。 它可以取零或非零值。 exit(0 )用于成功终止, exit(1)exit(-1)用于失败终止。 exit()方法不返回任何值。

Example:

例:

In this program, we are terminating the program based on a condition and using exit() method.

在此程序中,我们将根据条件并使用exit()方法终止该程序。

import java.util.*; 
import java.lang.*; 

class ExitDemo1
{ 
    public static void main(String[] args) 
    { 
        intx[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; 

        for (inti = 0; i<x.length; i++) 
        { 
            if (x[i] >= 40) 
            { 
                System.out.println("Program is Terminated..."); 
                System.exit(0); 
            } 
            else
                System.out.println("x["+i+"] = " + x[i]); 
        } 
    } 
}
system-exit-program

翻译自: https://www.studytonight.com/java/command-line-argument.php

java命令行参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值