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

java learning main function parameter passing

王林
王林forward
2019-12-05 17:45:103118browse

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”)

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);				
	}
}

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

Command line to compile java files:

javac Average.java

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

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete