java基础:获取输入字符串

来源:互联网 发布:英国传媒毕业 学编程 编辑:程序博客网 时间:2024/05/01 09:59
 
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;public class Test {public static void main(String[] args) throws UnsupportedEncodingException {BufferedReader br = null;InputStreamReader reader = new InputStreamReader(System.in);br = new BufferedReader(reader);String buffer = null;try {while((buffer = br.readLine()) != null){System.out.println("输入的内容为:"+buffer);}} catch (IOException e) {e.printStackTrace();}finally{try {br.close();} catch (IOException e) {e.printStackTrace();}}}}

还有另一种更简单的方法:

Scanner scanner = new Scanner(System.in);String inputStr = scanner.nextLine();System.out.println(inputStr);


 

 


 


 

原创粉丝点击