import java.io.IOException;class TestWhile{
public static void main(String[] args) throws IOException{
char response;
System.out.println("Are you felling better about " +
                          "programming?(Y or N)");
System.out.flush();
response=(char)System.in.read();
while ( response !='Y'&& response != 'N' ){
System.out.println("Invalid letter.");
System.out.println("Enter only 'Y' or 'N'");
response=(char)System.in.read(); 
//System.out.println("a" + response + "b");
} if(response =='Y')
System.out.println("I am grad.");
else
System.out.println("Keep trying!");
}
}
/*
但运行结果却是这样:
Are you felling better about programming?(Y or N)
d
Invalid letter.
Enter only 'Y' or 'N'
Invalid letter.
Enter only 'Y' or 'N'
Invalid letter.
Enter only 'Y' or 'N'
Y
I am grad. 为什么在输入d时,循环了了三次呢,,请说明原因及提出修改方法  
*/

解决方案 »

  1.   

    修改后的代码:import java.io.*;class TestWhile{
    public static void main(String[] args) throws IOException{
    String response;
    System.out.println("Are you felling better about " +
                              "programming?(Y or N)");
    System.out.flush();
    BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
    response=in.readLine();
    while ( !response.equals("Y") && !response.equals("N") ){
    System.out.println("Invalid letter.");
    System.out.println("Enter only 'Y' or 'N'");
    response=in.readLine(); 
    //System.out.println("a" + response + "b");
    } if(response.equals("Y"))
    System.out.println("I am grad.");
    else
    System.out.println("Keep trying!");
    }
    }
      

  2.   

    因为,你输入d之后回车,真实的输入是d\r\n,一共3个字符,呵呵
      

  3.   

    lz这里while ( response !='Y'&& response != 'N' )不能那样写,会出现短路
      

  4.   

    因为你在控制台输入字符'd'后,按回车键,系统默认在后面追加了\r\n,所以一共是三个字符,循环了三次。
    程序作如下修改可解决问题:import java.io.IOException;class TestWhile{
    public static void main(String[] args) throws IOException{
    char response;
    System.out.println("Are you felling better about " +
                              "programming?(Y or N)");
    System.out.flush();
    response=(char)System.in.read();
    while ( response !='Y'&& response != 'N' ){
    if(response=='\r' || response=='\n'){
    break;
    }
    System.out.println("Invalid letter.");
    System.out.println("Enter only 'Y' or 'N'");
    response=(char)System.in.read(); 
    //System.out.println("a" + response + "b");
    } if(response =='Y')
    System.out.println("I am grad.");
    else
    System.out.println("Keep trying!");
    }
    }
      

  5.   

    你输入的一个字符再加上回车 一共是三个字符 回车占两个 所以循环了三次。  用BufferedReader比较保险点
      

  6.   

    我也来一个,无限循环(Y,N选到害怕)哈哈!
    class TestWhile{
    public static void main(String[] args) 
    throws java.io.IOException{
    char response;
    do
    {
    System.out.println("Are you felling better about " + "programming?(Y or N)");
                                                                      
               do
               {            
           response=(char)System.in.read();
       }while (response=='\r'|response=='\n');
       
        if(response =='Y') System.out.println("I am grad.");
        else if(response=='N') System.out.println("Keep trying!");
    else
    {
       System.out.println("Invalid letter.");
       System.out.println("Enter only 'Y' or 'N'");
    }

    }while(response!='Y'|response!='N');
    }
    }