public static void main (String Args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String");
String s = br.readLine();
s=s+" ";
s.toLowerCase();
String word="";
String max="";
int count=0;
for(int i=0; i<s.length();i++){
char ch = s.charAt(i);
while(ch!=' ')
word+=ch;
if(word.length()>max.length()){
max=word; count++;
}
else count++;
}System.out.println(max+" , "+count);
}
}
我想在不使用 split 或类似的东西的情况下找到字符串中最大的单词,并计算句子中有多少个单词。 当我输入任何内容并按回车键时,什么也没有发生。有什么问题?
最佳答案
从控制台读取输入没有问题。
while(ch!=' ')
word+=ch;
它形成了一个无限循环。你应该像这样更新这个 while-loop
-
while(ch!=' '){
word+=ch;
ch = s.charAt(++i);
}
关于java - BufferedReader 即使在按回车后也没有接受输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18484113/