Home > Java > JavaBase > java learning main function parameter passing

java learning main function parameter passing

王林
Release: 2019-12-05 17:45:10
forward
3217 people have browsed it

java learning main function parameter passing

Objective

(1). Create a class and write the program source code in the editor;

Note:

The command line parameters are array args of String type in the main method. You can use the Integer.valueOf() method to convert the string into an integer type, for example:

int number= Integer.valueOf(“12321421”)
Copy after login

The parameters of this method can only be numeric strings

(2), compile the program;

(3), run the program.

Online video learning tutorial sharing: java free video tutorial

Experimental part

Experimental code:

public class Average {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//获取三个字符参数
		String one = args[0];
		String two = args[1];
		String three = args[2];
			
		//将三个字符参数转为浮点型
		double int_One = Double.valueOf(one).doubleValue();
		double int_Two = Double.valueOf(two).doubleValue();
		double int_Three = Double.valueOf(three).doubleValue();
		
		double average = (int_One + int_Two + int_Three) / 3;
		System.out.println("平均数为:"+average);				
	}
}
Copy after login

Program in MyEclipse, try the cmd command to compile and run.

Command line to compile java files:

javac Average.java
Copy after login

PS: javac is the command to compile java files, Average.java is your java file

command Run the java file:

java Average 3 4 5
Copy after login

PS: java is the command to run the java file, Average is the Java file name, and 3, 4, and 5 are the parameters to be passed to the main function.

Run results:

java learning main function parameter passing

Recommended related articles and tutorials: Getting started with java development

The above is the detailed content of java learning main function parameter passing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template