Java中用户向系统传递参数的三种基本方式

使用Main方法的参数传递方式

例示代码如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class MainArgs   
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         System.out.println(args.length);  
  6.         for(String str : args){  
  7.             System.out.println(str);  
  8.         }  
  9.     }  
  10. }  


在运行 java程序后面跟的字符串(一个或多个 多个用空格隔开)jvm将会把这些一个或多个字符串赋给args数组。当字符串中包含空格时则需要将完整的一个字符串用“”括起来。如下示例:

 

 

使用Scanner类进行用户输入:可以输入用户指定的数据类型

Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配。然后可以使用不同的 next 方法将得到的标记转换为不同类型的值。

例示代码如下:

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Scanner;  
  2. import java.io.File;  
  3. public class ScannerKeyBoardTest   
  4. {  
  5.     public static void main(String[] args) throws Exception  
  6.     {  
  7.         //readFileCon();  
  8.         //test2();  
  9.   
  10.         //通过键盘输入指定类型  
  11.         Scanner scan = new Scanner(System.in);  
  12.         Long l = scan.nextLong();  
  13.         System.out.println("l is "+l);  
  14.     }  
  15.     //读取任何的数据输入返回String  
  16.     public static void test1(){  
  17.         Scanner scan = new Scanner(System.in);  
  18.   
  19.         //使用 回车键 作为分隔符 默认使用 空格 制表键  回车作为分割付。  
  20.         //scan.useDelimiter("\n");    
  21.         while(scan.hasNext()){  
  22.             System.out.println("next is " + scan.next());  
  23.         }         
  24.     }  
  25.   
  26.     //读取Long型数据的输入返回Long  
  27.     public static void test2(){  
  28.         Scanner scan = new Scanner(System.in);  
  29.         //当输入的为 非 Long数值时 推出循环  
  30.         while(scan.hasNextLong()){//阻塞式  
  31.             //System.out.println("has over scan.nextLong() begin....");  
  32.             System.out.println("next is " + scan.nextLong());  
  33.             //System.out.println("scan.nextLong() over has begin....");  
  34.         }  
  35.     }  
  36.     //读取文件中的内容 并打印到控制台  
  37.     public static void readFileCon()throws Exception  
  38.     {  
  39.         Scanner scan  = new Scanner(new File("ScannerKeyBoardTest.java"));  
  40.         System.out.println("fileContent is:");  
  41.         while(scan.hasNextLine()){  
  42.             System.out.println(scan.nextLine());  
  43.         }  
  44.     }  
  45. }  
使用BufferedReader类读取用户的输入:返回的只能是String类

例示代码如下

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.BufferedReader;  
  2. import java.io.InputStreamReader;  
  3. class BufferReaderKeyBoardTest   
  4. {  
  5.     public static void main(String[] args) throws Exception  
  6.     {  
  7.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  8.         String in = null;  
  9.         while((in = br.readLine()) != null){  
  10.             System.out.println("用户输入的是: "+in);  
  11.         }         
  12.     }  
  13. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值