关于JAVA像C语言那样从控制台接收输入的字符的方法(一些代码转载)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 19:35   2072   0

一.最原始的方法—使用InputStreamReader和BufferedReader两个类

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

String s=br.readLine();

System.out.println("I found the text \"" + matcher.group() + "\" starting at index " + matcher.start() + " and ending at index " + matcher.end() + ".");
(JAVA中\是转义字符)

二.先进一点的方法—使用Scanner类

Scanner sca=new Scanner(System.in);

String s=sca.nextLine();

System.out.printf( "I found the text \"%s\" starting at index %d and ending at index %d.%n", matcher.group(), matcher.start(), matcher.end() );

三最先进的方法—使用Console类

Console col=System.console();

String s=console.readLine();

console.format("I found the text \"%s\" starting at index %d " + "and ending at index %d.%n", "something",10,100);

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP