Bài viết này cung cấp mã nguồn chương trình Java hiển thị cách để lấy một đối số (Argument) từ Command Line vào trong Java. Bạn có thể tham khảo thêm bài viết Argument là gì?
Following Java Program will show how to take Command Line Argument in JAVA.
UseArgument.java
Result - Kết Quả:
Following Java Program will show how to take Command Line Argument in JAVA.
UseArgument.java
/* @author vnlives.net */
/*****************************************************************
*
* Prints "Hi, Peter. How are you?" where "Peter" is replaced by the
* command-line argument.
*
*****************************************************************/
public class UseArgument {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
if (args.length > 0) {
System.out.print("Hi, ");
System.out.print(args[0]);
System.out.println(". How are you?");
}
}
}
/*****************************************************************
*
* Prints "Hi, Peter. How are you?" where "Peter" is replaced by the
* command-line argument.
*
*****************************************************************/
public class UseArgument {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
if (args.length > 0) {
System.out.print("Hi, ");
System.out.print(args[0]);
System.out.println(". How are you?");
}
}
}
Result - Kết Quả:
% javac UseArgument.java
% java UseArgument Bob
Hi, Bob. How are you?
% java UseArgument Bi
Hi, Bi. How are you?
% java UseArgument Bob
Hi, Bob. How are you?
% java UseArgument Bi
Hi, Bi. How are you?
No comments:
Post a Comment