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时,循环了了三次呢,,请说明原因及提出修改方法
*/
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时,循环了了三次呢,,请说明原因及提出修改方法
*/
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!");
}
}
程序作如下修改可解决问题: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!");
}
}
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');
}
}