对于老师讲的这个键盘录入的程序,我把'\r'和'\n'变成字符串后为什么就不行?
import java.io.*;
class ReadIn {
public static void main(String[] args) throws IOException {
InputStream in = System.in;
StringBuilder sb = new StringBuilder();
//以下代码块将做改动
while(true) {
int ch = in.read();
if (ch=='\r')
continue;
if(ch=='\n') {
String s = sb.toString();
if("over".equals(s))
break;
System.out.println(s.toUpperCase());
sb.delete(0,sb.length());
}
else
sb.append((char)ch);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
import java.io.*;
class ReadIn {
public static void main(String[] args) throws IOException {
InputStream in = System.in;
StringBuilder sb = new StringBuilder();
//以下代码块是做改动后的
char[] c = {'\r'};
String str = new String(c);
char[] c1 = {'\n'};
String str1 = new String(c1);
while(true) {
int ch = in.read();
if (str.equals(ch))
continue;
if(str1.equals(ch)) {
String s = sb.toString();
if("over".equals(s))
break;
System.out.println(s.toUpperCase());
sb.delete(0,sb.length());
}
else
sb.append((char)ch);
}
}
}
|
|