[英]BufferedReader not taking input even after pressing enter
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);
}
你在那里有無限循環
while(ch!=' ')
word+=ch;
伙計,它可以工作,readLine中沒有錯誤。
但是我看到一個無限循環:
while(ch!=' ')
word+=ch;
請檢查一次邏輯...
public class LengthiestWord {
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;
ch = s.charAt(++i);
}
if(word.length()>max.length()){
max=word;
word="";
count++;
}
else {
count++;
word="";
}
}
System.out.println(max+" , "+count);
}
}
O / P ---- >>>>
輸入字符串
所有錯誤均已修復
錯誤4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.