zoukankan      html  css  js  c++  java
  • Java获取用户输入

    转自:http://blog.csdn.net/zhy_cheng/article/details/7875094

    1.使用Scanner

    1. import java.util.Scanner;  
    2. public class Test {  
    3.     public static void main(String[] args) {  
    4.           
    5.         Scanner sc=new Scanner(System.in);  
    6.         while(sc.hasNext())  
    7.         {  
    8.             System.out.println("输出:"+sc.next());  
    9.         }  
    10.   
    11.     }  
    12. }  
    import java.util.Scanner;
    public class Test {
    	public static void main(String[] args) {
    		
    		Scanner sc=new Scanner(System.in);
    		while(sc.hasNext())
    		{
    			System.out.println("输出:"+sc.next());
    		}
    
    	}
    }
    

    使用Scanner的方式获取用户的输入的话,Scanner默认使用空格,Tab,回车作为输入项的分隔符。

    可以使用sc.useDelimiter()方法来改变这种默认。

    sc可以读取特定的数据,比如int,long看下图

    从图中可以看到nextBoolean,nextFloat等等。

    Scanner提供一个简单的方法一行一行的读取

    1. import java.util.Scanner;  
    2. public class Test {  
    3.     public static void main(String[] args) {  
    4.           
    5.         Scanner sc=new Scanner(System.in);  
    6.           
    7.         while(sc.hasNextLine())  
    8.         {  
    9.             System.out.println("输出:"+sc.nextLine());  
    10.         }  
    11.   
    12.     }  
    13. }  
    import java.util.Scanner;
    public class Test {
    	public static void main(String[] args) {
    		
    		Scanner sc=new Scanner(System.in);
    		
    		while(sc.hasNextLine())
    		{
    			System.out.println("输出:"+sc.nextLine());
    		}
    
    	}
    }
    


    Scanner不仅可以读取用户键盘的输入,也可以读取文件

    1. import java.io.File;  
    2. import java.util.Scanner;  
    3.   
    4. public class Test {  
    5.     public static void main(String[] args) throws Exception  
    6.     {  
    7.           
    8.         Scanner sc=new Scanner(new File("C:\Users\zhycheng\Desktop\Dota超神\描述.txt"));  
    9.           
    10.         while(sc.hasNextLine())  
    11.         {  
    12.             System.out.println("输出:"+sc.nextLine());  
    13.         }  
    14.   
    15.     }  
    16. }  
    import java.io.File;
    import java.util.Scanner;
    
    public class Test {
    	public static void main(String[] args) throws Exception
    	{
    		
    		Scanner sc=new Scanner(new File("C:\Users\zhycheng\Desktop\Dota超神\描述.txt"));
    		
    		while(sc.hasNextLine())
    		{
    			System.out.println("输出:"+sc.nextLine());
    		}
    
    	}
    }
    


    2.使用BufferedReader

    需要指出的是Scanner是Java5提供的工具类,在Java5之前使用BufferedReader来读取

      1. import java.io.BufferedReader;  
      2. import java.io.InputStreamReader;  
      3.   
      4. public class Test {  
      5.     public static void main(String[] args) throws Exception  
      6.     {  
      7.           
      8.         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
      9.         String str=null;  
      10.         while((str=br.readLine())!=null)  
      11.         {  
      12.             System.out.println(str);  
      13.         }  
      14.   
      15.     }  
  • 相关阅读:
    阻塞队列(BlockingQueue)
    CountDownLatch/CyclicBarrier/Semaphore
    Guava Cache详解
    Java中的常见锁(公平和非公平锁、可重入锁和不可重入锁、自旋锁、独占锁和共享锁)
    CopyOnWriteArrayList详解
    集合类线程安全问题
    原子类的 ABA 问题
    原子类CAS的底层实现
    volatile关键字
    Java 内存模型(Java Memory Model,JMM)
  • 原文地址:https://www.cnblogs.com/1996313xjf/p/6078384.html
Copyright © 2011-2022 走看看