Java BufferedReader - 最后一行输出不会自动读取,只有当我按下输入

时间:2015-05-25 12:56:47

标签: java io bufferedreader bufferedinputstream

这是我的代码:

public class hack 
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader sc = new  BufferedReader(new InputStreamReader(System.in));
        int count = 0;
        int cases = Integer.parseInt(sc.readLine());
        String pie = "31415926535897932384626433833";
        String str;
        for(int i = 0; i < cases; i++)
        {
            str = sc.readLine();
            String []strarr = str.split(" ");
            String str1 = "";
            for(int k = 0; k < strarr.length; k++)
            {
                char check[] = strarr[k].toCharArray();
                count = 0;
                for(int j = 0; j < strarr[k].length(); j++)
                {
                    if((check[j] > 64 && check[j] < 91) || (check[j] > 96 && check[j] < 122))
                    {
                        count++;
                    }
                }
                str1 = str1 + count;
            }
            if(str1.equalsIgnoreCase(pie.substring(0,strarr.length)))
            {
                System.out.println("It's a pi song.");
            }
            else
            {
               System.out.println("It's not a pi song.");
            }
        }
    }
}

输入文件如下:

4<br>
dHjVPihCZ BjHi OkWbQBH NIQM Ue BPIJHS ZdWQLMIxy wzVGBhx GqQjKMeJ ZmfZo l qmDWBUrs<br>
YkoGQTJYN vgy tJ k B g YV T iXExLPfFk eL TTcOtic MRbmKE<br>
RMSIYuD GiRYQ U K THGUT XYycRULD pTG NqlHUO RFEG rJ sqcGVd jZ IhTlGUvE FLQfISMqN cW<br>
Rzm YPPGEQDXq PsDpNLCd qLTFiFDn wFqpGvSB YQqIZY sfWRwGJ<br><br>

输出如下:

dHjVPihCZ BjHi OkWbQBH NIQM Ue BPIJHS ZdWQLMIxy wzVGBhx GqQjKMeJ ZmfZo l qmDWBUrs<br>
YkoGQTJYN vgy tJ k B g YV T iXExLPfFk eL TTcOtic MRbmKE<br>
RMSIYuD GiRYQ U K THGUT XYycRULD pTG NqlHUO RFEG rJ sqcGVd jZ IhTlGUvE FLQfISMqN cW<br>
It's not a pi song.<br>
It's not a pi song.<br>
It's not a pi song.<br>
Rzm YPPGEQDXq PsDpNLCd qLTFiFDn wFqpGvSB YQqIZY sfWRwGJ<br>
it's not a pi song.
<br><br>

在这里我给了4作为没有。的情况。作为输入,我将所有四条线组合在一起,但它只考虑前三条线作为输入,只有它们的输出一起显示,我希望所有四条线的输出一起显示。

1 个答案:

答案 0 :(得分:0)

您需要在最后一个输入行后添加另一个换行符。 BufferedReader.readLine() javadoc:

  

读取一行文字。一条线被认为是由换行符('\ n'),回车符('\ r')或回车符后面的任何一个终止。