java中从键盘输入的三种方法以及Console输入

java中从键盘输入的三种方法:

import java.io.BufferedReader;    
import java.io.IOException;    
import java.io.InputStreamReader;    
import java.util.Scanner;    

public class Inout {   
      
  public static void main(String[] args) {
    charTest();  //调用System.in方法   
    readTest();  //调用ReadTest方法   
    scannerTest();//调用ScannerTest方法   
  }   
  /**  
   * System.in和System.out方法  
   * 缺点一: 该方法能获取从键盘输入的字符,但只能针对一个字符的获取  
   * 缺点二: 获取的只是char类型的。如果想获得int,float等类型的输入,比较麻烦。  
   */  
  public static void charTest(){    
    try{   
      System.out.print("Enter a Char:");   
      char i = (char)System.in.read();   
      System.out.println("Yout Enter Char is:" + i);   
    }   
    catch(IOException e){   
      e.printStackTrace();   
    }   
        
  }   
  /**  
   * InputStreamReader和BufferedReader方法  
   * 优点: 可以获取键盘输入的字符串  
   * 缺点: 如何要获取的是int,float等类型的仍然需要转换  
   */  
  public static void readTest(){   
    System.out.println("ReadTest, Please Enter Data:");   
    InputStreamReader is = new InputStreamReader(System.in);  //new构造InputStreamReader对象   
    BufferedReader br = new BufferedReader(is);  //拿构造的方法传到BufferedReader中   
    try{  //该方法中有个IOExcepiton需要捕获   
      String name = br.readLine();   
      System.out.println("ReadTest Output:" + name);   
    }   
    catch(IOException e){   
      e.printStackTrace();   
    }   
        
  }   
  /**  
   * Scanner类中的方法  
   * 优点一: 可以获取键盘输入的字符串  
   * 优点二: 有现成的获取int,float等类型数据,非常强大,也非常方便;  
   */  
  public static void scannerTest(){   
    Scanner sc = new Scanner(System.in);   
    System.out.println("ScannerTest, Please Enter Name:");   
    String name = sc.nextLine();  //读取字符串型输入   
    System.out.println("ScannerTest, Please Enter Age:");   
    int age = sc.nextInt();  //读取整型输入   
    System.out.println("ScannerTest, Please Enter Salary:");   
    float salary = sc.nextFloat();  //读取float型输入   
    System.out.println("Your Information is as below:");   
    System.out.println("Name:" + name +"\n" + "Age:" + age + "\n"+"Salary:" + salary);
  }   
} 

Console输入: (注:该种方法不能在eclipse中使用)

import java.io.Console;
import java.io.PrintWriter;

public class TestConsole {
    public static void main(String[] args) {
        
        Console cons = System.console();
        if (cons != null) {
            
            PrintWriter printWriter = cons.writer();
            printWriter.write("input:");
            cons.flush();
            String str1 = cons.readLine();
            cons.format("%s", str1);
        }
    }
}

 

转载自:http://blog.csdn.net/u012249177/article/details/49586383

转载于:https://www.cnblogs.com/lbd_smile/p/8477406.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值