日期:2014-05-20  浏览次数:20828 次

急!急!急求答案!
我写了一个程序打算从键盘输入一个数然后在读入这个数到程序中,在接着输入一个数在读入,用下面方法怎么就是有问题:
      System.out.println( "the   animal   doesn 't   fly?   In(Y/N) ");
        try   {boolean   t4_1=System.in.read(   )==(int)( 'Y ');//运行到这里CMD光标提示输入内容,从键盘输入Y   ,按回车接着下面(2)
                 
        System.out.println( "the   animal   doesn 't   layegg?   In(Y/N) ");//(2)然后屏幕输出这个,却没有光标提示从键盘输入(Y/N),   无法输入直接就运行下去了
                boolean   t4_2=System.in.read(   )==(int)( 'Y ');
           
                if(t4_1&&t4_2)   {
                fly=true;   layEgg=true;  
                rule4(fly,layEgg);
                }
                }catch(IOException   e)  
            {     System.out.println( "Error   reading   from   user ");          
            }



------解决方案--------------------
你输入一个字符后,又输入了一个回车,这时System.in里面有两个字符,所以可以读两次。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
用这个方法去读
------解决方案--------------------
[code=Java][/code]
import java.util.*; 
public class Scan { 
public static void main(String[] args) { 
Scanner scann = new Scanner(System.in); 
int i = scann.nextInt(); 
System.out.print(i); 


}
------解决方案--------------------
支持2楼的方法