代码示例:
/* * 标准的输入输出流 * 标准的输出流:System.out * 标准的输入流:System.in * * 题目: * 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作, * 直至当输入“e”或者“exit”时,退出程序。 * */ @Test public void test2() { BufferedReader br = null; try { InputStream is = System.in; InputStreamReader isr = new InputStreamReader(is); br = new BufferedReader(isr); String str; while (true) { System.out.println("请输入字符串:"); str = br.readLine(); if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")) { break; } String str1 = str.toUpperCase(); System.out.println(str1); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
练习:
public class MyInput { public String nextString() { BufferedReader br = null; String str = null; try { InputStreamReader isr = new InputStreamReader(System.in); br = new BufferedReader(isr); str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return str; } public int nextInt() { return Integer.parseInt(nextString()); } public boolean nextBoolean() { return Boolean.parseBoolean(nextString()); } public static void main(String[] args) { MyInput i = new MyInput(); System.out.println("请输入一个字符串:"); String s = i.nextString(); System.out.println(s); // System.out.println("请输入一个布尔值:"); // Boolean s = i.nextBoolean(); // System.out.println(s); } }