高分求解决一个java问题!我就剩下40分了,都给了,呵呵。

wangming7306 2007-06-30 06:58:03
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时,循环了了三次呢,,请说明原因及提出修改方法
*/
...全文
297 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainv 2007-06-30
  • 打赏
  • 举报
回复
好像是每天回一次贴可以加10分。
lz不要只会索取,没有奉献哦。
javaflash9 2007-06-30
  • 打赏
  • 举报
回复
我也来一个,无限循环(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');
}
}
joejoe1991 2007-06-30
  • 打赏
  • 举报
回复
你输入的一个字符再加上回车 一共是三个字符 回车占两个 所以循环了三次。 用BufferedReader比较保险点
恭常章 2007-06-30
  • 打赏
  • 举报
回复
因为你在控制台输入字符'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!");
}
}
wangming7306 2007-06-30
  • 打赏
  • 举报
回复
完了,我以后没分在发帖子了,嘿嘿
jefyjiang 2007-06-30
  • 打赏
  • 举报
回复
lz这里while ( response !='Y'&& response != 'N' )不能那样写,会出现短路
jefyjiang 2007-06-30
  • 打赏
  • 举报
回复
正解
zqrqq 2007-06-30
  • 打赏
  • 举报
回复
用readLine会比较保险
zqrqq 2007-06-30
  • 打赏
  • 举报
回复
因为,你输入d之后回车,真实的输入是d\r\n,一共3个字符,呵呵
huoyin 2007-06-30
  • 打赏
  • 举报
回复
修改后的代码:

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!");
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧