需求:從鍵盤輸入字符串,將讀到的整行字符串轉化成大寫輸出,然後繼續進行輸入操作,直到當輸入"e" 或者"exit"時,退出程序。
代碼:
package com.yang.otherStream;
import java.io.*;
public class Demo01 {
public static void main(String[] args) {
System.out.println("請輸入字符串:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
try {
while ((line = br.readLine()) != null){
if("e".equalsIgnoreCase(line) || "exit".equalsIgnoreCase(line)){
System.out.println("安全退出");
break;
}else{
System.out.println(line.toUpperCase());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
結果: