本文介紹了BufferedReadLine()接受無限輸入…為什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
為什么我的BufferedReaderreadLine()方法接受無限輸入
如何解決這個問題…如果有人在程序中發現任何錯誤…..歡迎回復
import java.io.*;
import java.util.*;
public class average {
public static void main(String[] args){
int j,sum=0,count=0;
String[] input=null;
System.out.println("Enter the number of test cases");
Scanner in=new Scanner(System.in);
int t=Integer.parseInt(in.nextLine());
System.out.println(t);
System.out.println("Enter the number of contestent and there score");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
try {
String line = stdin.readLine();
System.out.print('
');
while (line != null && line.length()!= 0 )
input = line.split(" ");
} catch (IOException e) {
System.out.println("Something wrong happend try again"+e);
}
for(int i=1;i<=input.length;i++)
sum+=Integer.parseInt(input[i]);
System.out.println(sum);
int avg=sum/(Integer.parseInt(input[0]));
for (int i=1;i<=input.length;i++)
{
if(Integer.parseInt(input[i])==avg)
count++;
}
System.out.println("The number of contestent who got average marks is"+count );
}
}
推薦答案
while (line != null && line.length()!= 0 )
input = line.split(" ");
這是一個無限循環。如果line不為Null且不為空,則循環內的任何內容都不會改變這一點,因此它將無限期地循環。
這篇關于BufferedReadLine()接受無限輸入…為什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,






