简体   繁体   English

通过参数将值传递给我的程序是什么意思?

[英]What does it mean to pass a value through an argument to my program?

I recently made a post saying that my code wasn't working and displayed the message:我最近发了一个帖子说我的代码不起作用并显示了消息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at FactorialProgram5.main(FactorialProgram5.java:11)

with the code:使用代码:

import java.util.Scanner;

public class FactorialProgram5 {

    public static void main(String args[]) {
        long n;
        long fact = 1;
        n = 1;
        
        for (int i = 1; i <= n; i++) {
            n = Long.parseLong(args[0]);
            fact = fact * i;
        }

        System.out.print("fact=" + fact);
    }
}

Someone said I have to pass arguments to it and that the array is empty.有人说我必须将 arguments 传递给它,并且数组是空的。 What does it mean to pass an argument to the code?将参数传递给代码是什么意思? I am using eclipse, and someone told me that I have to go to the arguments tab and enter a value manually, but I need to make it so I can use this code in the command prompt too.我正在使用 eclipse,有人告诉我,我必须 go 到 arguments 选项卡并手动输入一个值,但我也需要在命令提示符下使用此代码。 How can I do this?我怎样才能做到这一点?

Your main method is what takes any arguments from the command line.您的主要方法是从命令行获取任何 arguments 。 For example if you first navigate to the location of your java file on the command line: Type these commands.例如,如果您首先在命令行上导航到 java 文件的位置: 键入这些命令。 1. javac filename.java this will compile your.java file 2. java filename (if you want to run without arguments). 1. javac filename.java .java 这将编译你的.java 文件 2. java filename (如果你想不带参数运行)。 To pass arguments to your file use: java filename argument要将 arguments 传递给您的文件,请使用: java filename argument

Here argument is turned into a String array (args in the main method).这里的参数变成了一个字符串数组(主方法中的 args)。

So when this file is compiled an then ran with something like the command java filename 99 the line n=Long.parseLong(args[0]);所以当这个文件被编译然后运行类似命令java filename 99n=Long.parseLong(args[0]); in your code will plug in the number 99 to args[0].在您的代码中将数字 99 插入 args[0]。 So n will get set to 99, or whatever you decide to type after java filename on the command line each time you run this program.因此,每次运行此程序时,n 将设置为 99,或者您决定在命令行上的java filename之后键入的任何内容。

What that guy was talking about is that in the command line you can type in arguments when running the java program, kind of like this:那家伙说的是,在运行 java 程序时,你可以在命令行中输入 arguments,有点像这样:

<run-java-using-a-command> Bob Joe Bill

I omitted the actual running since that isn't necessary if you're just going to use eclipse.我省略了实际运行,因为如果您只是要使用 eclipse,则没有必要这样做。 But if you run it like this, then the args array will be this:但是如果你像这样运行它,那么args数组将是这样的:

[Bob, Joe, Bill]

You can use those arguments in your program.您可以在程序中使用这些 arguments。 In Eclipse, what you can do is click Run | Run | Program Arguments在 Eclipse 中,您可以点击Run | Run | Program Arguments Run | Run | Program Arguments Run | Run | Program Arguments (on the top of the screen) to edit arguments. Run | Run | Program Arguments (在屏幕顶部)编辑 arguments。

It means that you need to add a parameter on the command line so your program will find and process it.这意味着您需要在命令行上添加一个参数,以便您的程序找到并处理它。 The parameter I am using in this example is the number 3.我在这个例子中使用的参数是数字 3。

I have compiled and run your program on the command line to show how it works.我已经在命令行上编译并运行了你的程序来展示它是如何工作的。

$ javac FactorialProgram5.java
$ java FactorialProgram5 3    
fact=6
$

Note: I added a missing newline to the print instruction注意:我在打印指令中添加了一个缺少的换行符

System.out.print("fact=" + fact);

becomes变成

System.out.print("fact=" + fact + "\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM