dポイントプレゼントキャンペーン実施中!

入力された文字をそのまま表示し、[Ctrl]+Zが入力されると終了するプログラムを組んでいるんですがどのように記述すればよいか困っています。アドバイスお願いします。

public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;

do{
str = br.readLine();
System.out.println(str);
}while(str == null);
}

A 回答 (1件)

Sampleを示します。


Ctrl+Zの場合は特殊なケースなのでcが-1のとき
終了します。SampleではCtrl+Xのとき終了します。
class Test1023 {
public static void main(String[] args)
throws java.io.IOException { // 入出力エラーがありうる
while (true) { // 以下を繰り返す:
int c = System.in.read(); // キー入力を c に代入
if (c == 24) break; // c が 24(^x) ならループ脱出
System.out.println(c); // c を出力
}
}
}

参考URL:http://oku.edu.mie-u.ac.jp/~okumura/java2/io.html
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!