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

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  //读取任何的数据输入返回String

  public static void test1(){

  Scanner scan = new Scanner(System.in);

  //使用 回车键 作为分隔符 默认使用 空格 制表键 回车作为分割付。

  //scan.useDelimiter(“\n”);

  while(scan.hasNext()){

  System.out.println(“next is ” + scan.next());

  }

  }

  //读取Long型数据的输入返回Long

  public static void test2(){

  Scanner scan = new Scanner(System.in);

  //当输入的为 非 Long数值时 推出循环

  while(scan.hasNextLong()){//阻塞式

  //System.out.println(“has over scan.nextLong() begin….”);

  System.out.println(“next is ” + scan.nextLong());

  //System.out.println(“scan.nextLong() over has begin….”);

  }

  }

  //读取文件中的内容 并打印到控制台

  public static void readFileCon()throws Exception

  {

  Scanner scan = new Scanner(new File(“ScannerKeyBoardTest.java”));

  System.out.println(“fileContent is:”);

  while(scan.hasNextLine()){

  System.out.println(scan.nextLine());

  }

  }

  }

  使用BufferedReader类读取用户的输入:返回的只能是String类

  例示代码如下

  import java.io.BufferedReader;

  import java.io.InputStreamReader;

  class BufferReaderKeyBoardTest

  {

  public static void main(String[] args) throws Exception

  {

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  String in = null;

  while((in = br.readLine()) != null){

  System.out.println(“用户输入的是: “+in);

  }

  }

  }

[1][2]

教育人的诗句或名言警句,激励人在逆境中脱颖而出的话

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

相关文章:

你感兴趣的文章:

标签云: