新手遇到基本问题,请指教

stud3x 2009-07-24 04:06:09
//用do-while实现菜单功能的例题
class Menu{
public static void main (String args[])
throws java.io.IOException{
char c;
System.out.println("输入关键字前的数字可查看相应的格式:");
do{
System.out.println("1.if");
System.out.println("2.switch");
System.out.println("3.while");
System.out.println("4.do-while");
System.out.println("5.for");
System.out.println("0.退出 \n");
System.out.print("请输入:\n");
c = (char) System.in.read();
if (c<'0'||c>'5')
{System.out.println("\n\n输入错误!请输入关键字前面的数字\n"); }//为什么输入1-5也执行这条语句? else{
switch(c){
case'1':
System.out.println("if的格式为:\n");
System.out.println("if(条件)语句体;");
System.out.println("else 语句体");
break;
case'2':
System.out.println("switch的格式为:\n");
System.out.println("switch(表达式){");
System.out.println("case 常量:");
System.out.println("语句体序列");
System.out.println("break;");
System.out.println("//……");
System.out.println("}");
break;
case'3':
System.out.println("while的格式为:\n");
System.out.println("while(条件)语句体;");
break;
case'4':
System.out.println("do-while的格式为:\n");
System.out.println("do {");
System.out.println("语句体;");
System.out.println("}while(条件);");
break;
case'5':
System.out.println("for的格式为:\n");
System.out.println("for(初值;条件;迭代)");
System.out.println("语句体;");
break;
}System.out.println("输入的值是:"+c+"\n"); //输入1-5,此行不显示
}System.out.println("输入的值是:"+c+"\n"); //输入1-5,此行只显示汉字?输入的值呢,为什么不显示?
}while(c!='0');//输入不等于0的值时将重复出现该菜单。
}
}
...全文
189 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuweifeng0323 2009-07-26
  • 打赏
  • 举报
回复
我帮你运行了下,结果如下
输入关键字前的数字可查看相应的格式:
1.if
2.switch
3.while
4.do-while
5.for
0.退出

请输入:
1
if的格式为:

if(条件)语句体;
else 语句体
输入的值是:1

输入的值是:1

1.if
2.switch
3.while
4.do-while
5.for
0.退出

请输入:


输入错误!请输入关键字前面的数字

输入的值是:

1.if
2.switch
3.while
4.do-while
5.for
0.退出

请输入:


输入错误!请输入关键字前面的数字

输入的值是:


1.if
2.switch
3.while
4.do-while
5.for
0.退出

请输入:


我没学过IO,我输入都用SCANNER的,运行结果前部分是正确的,后部分多余了,我想可能如7楼所说的那样

PS:这格式很头痛
stud3x 2009-07-26
  • 打赏
  • 举报
回复
我犯了个超级低级的错误!
其实无论是1楼的例题还是13楼的原题,输入0-5之外的数字时,程序都循环执行了3次(因为接受了3个字符),但我一直没注意到,7楼、12楼的学长提醒我了我还只盯着最后一次输出看,直到今早开两个窗口同时分别运行2个例题对比输出才发现(这几天眼珠子开小差了)。
再次感谢各位学长的耐心指导,谢谢!
xuweifeng0323 2009-07-26
  • 打赏
  • 举报
回复
原题正确是因为他只做一次SWITCH,也就是如果你输入的不是1到5的数字,那么他重复显示
输入关键字前的数字可查看相应的格式:
1.if
2.switch
3.while
4.do-while
5.for
0.退出
而你如果输入正确的话,他执行一次就退出了,其实原题写的不好,不信你输个错误的数字,他显示上面那段3次,如:
输入关键字前的数字可查看相应的格式:
1.if
2.switch
3.while
4.do-while
5.for
0.退出

请输入:
8
输入关键字前的数字可查看相应的格式:
1.if
2.switch
3.while
4.do-while
5.for
0.退出

请输入:
输入关键字前的数字可查看相应的格式:
1.if
2.switch
3.while
4.do-while
5.for
0.退出

请输入:
输入关键字前的数字可查看相应的格式:
1.if
2.switch
3.while
4.do-while
5.for
0.退出

请输入:
原因应该就是7楼所说的
cwjieNo1 2009-07-25
  • 打赏
  • 举报
回复
那里有 do if的结构啊
holsten32 2009-07-25
  • 打赏
  • 举报
回复
stud3x 2009-07-25
  • 打赏
  • 举报
回复
如果按7楼、12楼的说法,假如我输入3,回车后实际输入的是3和2个其它字符;程序读取了这3个字符中的第1个字符(3),那么程序第一次的运行结果应该是正常的,但实际上却执行了{System.out.println("\n\n输入错误!请输入关键字前面的数字\n"); ,可是输入字符0却能正常输出和运行。
也就是说,这程序只在输入为0时才正常运行,其它值一概执行“输入错误!”那行。
stud3x 2009-07-25
  • 打赏
  • 举报
回复
首先感谢各位学长的解答,但我还是不明白,因为原例题这样读取字符是可行的,下面附上原例题:
//用do-while实现菜单功能的例题
class Menu1{
public static void main (String args[])
throws java.io.IOException{
char c;
do{
System.out.println("输入关键字前的数字可查看相应的格式:");
System.out.println("1.if");
System.out.println("2.switch");
System.out.println("3.while");
System.out.println("4.do-while");
System.out.println("5.for");
System.out.println("0.退出 \n");
System.out.print("请输入:\n");
c = (char) System.in.read();
}while(c<'0'||c>'5');
System.out.print("\n");
switch(c){
case'0':
break;
case'1':
System.out.println("if的格式为:\n");
System.out.println("if(条件)语句体;");
System.out.println("else 语句体");
break;
case'2':
System.out.println("switch的格式为:\n");
System.out.println("switch(表达式){");
System.out.println("case 常量:");
System.out.println("语句体序列");
System.out.println("break;");
System.out.println("//……");
System.out.println("}");
break;
case'3':
System.out.println("while的格式为:\n");
System.out.println("while(条件)语句体;");
break;
case'4':
System.out.println("do-while的格式为:\n");
System.out.println("do {");
System.out.println("语句体;");
System.out.println("}while(条件);");
break;
case'5':
System.out.println("for的格式为:\n");
System.out.println("for(初值;条件;迭代)");
System.out.println("语句体;");
break;
}
}
}
kakagui 2009-07-24
  • 打赏
  • 举报
回复
第一次输入1-5中的某一个数,按回车(实际是输入了三个字符),read方法仅读取第一个字符,输出正常的结果;进行第二次循环的时候,缓存区内还有字符,故继续读取,此次读取的字符是回车符,所以程序跑到if中,第三次循环,缓存区内仍然有字符,继续读取此次读取的字符是换行符,仍跑到if中,第四次的时候就会提示你再次输入。
stud3x 2009-07-24
  • 打赏
  • 举报
回复
难道要认为的去掉 多余的字节\r\n?
但原例题是可以的,原题没有if语句,且switch语句在循环外,所以这样接受输入应该可行
smallbear923 2009-07-24
  • 打赏
  • 举报
回复
肯定是C的问题,打印下他的值。。
junjun1984 2009-07-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xlxyeyu 的回复:]
c = (char) System.in.read();

你输入了数字5,转化成char型后就不一定是'5'了!
[/Quote]
比较的是ASC码
stud3x 2009-07-24
  • 打赏
  • 举报
回复
原例题是可以的,原题没有if语句,且switch语句在循环外,所以接受的输入应该是没问题的
alpha_423 2009-07-24
  • 打赏
  • 举报
回复
原因找到了。c = (char) System.in.read();这边,System.in里有3个字节:
你输入的数字\r\n
xlxyeyu 2009-07-24
  • 打赏
  • 举报
回复
建议打印出来看看!
xlxyeyu 2009-07-24
  • 打赏
  • 举报
回复
c = (char) System.in.read();

你输入了数字5,转化成char型后就不一定是'5'了!
stud3x 2009-07-24
  • 打赏
  • 举报
回复
后面加的2行System.out.println("输入的值是:"+c+"\n"); 就是想知道到底读了什么值的,可只有0能显示,其它的都没显示(如注释所说)
flyxxxxx 2009-07-24
  • 打赏
  • 举报
回复
c = (char) System.in.read();
你检查一下你这行读到的到底是什么?问题肯定在这里。
stud3x 2009-07-24
  • 打赏
  • 举报
回复
class前加上public 结果和之前一样
flyxxxxx 2009-07-24
  • 打赏
  • 举报
回复
请把class前加上public

62,615

社区成员

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

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