DEITLE 书中的简单程序,读取 5 个整数,程序应打印包含该数量相邻星号的行。即(如果 num = 5 则输出 *****)。我已经在 c++ 中测试了相同的解决方案,并且效果很好。然而,这里的缓冲区持有奇怪的值 (50 ?) 。我认为问题在于使用缓冲区,但是我想知道为什么会这样?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.io.*;
import java.util.Date;
/**
* Program that reads five numbers and print starts equal to each number
*
* @author Hassan
*/
public class JavaApplication1
{
public static void main(String[] args)
{
int num ;
java.io.InputStreamReader ins= new InputStreamReader(System.in);
java.io.BufferedReader bfr = new BufferedReader (ins);
try
{
for(int i=0;i<5;i++)
{
System.out.println("Please Enter a number");
num = bfr.read();
System.out.print(num);
for(int j=0;j<num;j++)
{ System.out.print("* "); }
System.out.println("\n");
}
}
catch(Exception E )
{System.out.println(E.getMessage()); }
}
}
编辑:问题已解决,但程序 - 出 5 个输入 - 仅读取 3 个,即其读取输入并忽略下一个