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

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

1import java.io.BufferedReader; 2import java.io.IOException; 3import java.io.InputStreamReader; 4import java.util.Scanner; 5 6public class Inout { 7 8 public static void main(String[] args) { 9 charTest(); //调用System.in方法 10 readTest(); //调用ReadTest方法 11 scannerTest();//调用ScannerTest方法 12 } 13 /** 14 * System.in和System.out方法 15 * 缺点一: 该方法能获取从键盘输入的字符,但只能针对一个字符的获取 16 * 缺点二: 获取的只是char类型的。如果想获得int,float等类型的输入,比较麻烦。 17 */ 18 public static void charTest(){ 19 try{ 20 System.out.print("Enter a Char:"); 21 char i = (char)System.in.read(); 22 System.out.println("Yout Enter Char is:" + i); 23 } 24 catch(IOException e){ 25 e.printStackTrace(); 26 } 27 28 } 29 /** 30 * InputStreamReader和BufferedReader方法 31 * 优点: 可以获取键盘输入的字符串 32 * 缺点: 如何要获取的是int,float等类型的仍然需要转换 33 */ 34 public static void readTest(){ 35 System.out.println("ReadTest, Please Enter Data:"); 36 InputStreamReader is = new InputStreamReader(System.in); //new构造InputStreamReader对象 37 BufferedReader br = new BufferedReader(is); //拿构造的方法传到BufferedReader中 38 try{ //该方法中有个IOExcepiton需要捕获 39 String name = br.readLine(); 40 System.out.println("ReadTest Output:" + name); 41 } 42 catch(IOException e){ 43 e.printStackTrace(); 44 } 45 46 } 47 /** 48 * Scanner类中的方法 49 * 优点一: 可以获取键盘输入的字符串 50 * 优点二: 有现成的获取int,float等类型数据,非常强大,也非常方便; 51 */ 52 public static void scannerTest(){ 53 Scanner sc = new Scanner(System.in); 54 System.out.println("ScannerTest, Please Enter Name:"); 55 String name = sc.nextLine(); //读取字符串型输入 56 System.out.println("ScannerTest, Please Enter Age:"); 57 int age = sc.nextInt(); //读取整型输入 58 System.out.println("ScannerTest, Please Enter Salary:"); 59 float salary = sc.nextFloat(); //读取float型输入 60 System.out.println("Your Information is as below:"); 61 System.out.println("Name:" + name +"\n" + "Age:" + age + "\n"+"Salary:" + salary); 62 } 63}

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

1import java.io.Console; 2import java.io.PrintWriter; 3 4public class TestConsole { 5 public static void main(String[] args) { 6 7 Console cons = System.console(); 8 if (cons != null) { 9 10 PrintWriter printWriter = cons.writer(); 11 printWriter.write("input:"); 12 cons.flush(); 13 String str1 = cons.readLine(); 14 cons.format("%s", str1); 15 } 16 } 17}

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

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

UIPath入门系列六之UI自动化进阶

UI自动化进阶一、InputActions:点击,键盘输入,快捷键,右键,鼠标悬停等1)   Default(默认):使用鼠标和键盘驱动程序来模拟人的操作,缺点是要求应用程序窗口保存活动的状态2)   SimulateType/Click(模拟):三种方法中最快的,后台工作,不支持快捷键,可自

javaDoc

我们知道Java中有三种注释语句:1.//用于单行注释。2./\...\/用于多行注释,从/\开始,到\/结束,不能嵌套。3./\\...\/则是为支持jdk工具javadoc.exe而特有的注释语句。   javadoc工具能从java源文件中读取第三种注释,并能识别注释中用@标识的一些特殊变量(见表),制作成Htm

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

NIO之Buffer的clear()、rewind()、flip()方法的区别

Java的NIO中有关Buffer的几种常用方法比如clear,rewind和flip到底有哪些区别。下面给大家这三种方法的源码,方便大家记忆。clear()方法用于写模式,其作用为情况Buffer中的内容,所谓清空是指写上限与Buffer的真实容量相同,即limitcapacity,同时将当前写位置置为最前端下标为0处。代码如下:1.pu