//用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的值时将重复出现该菜单。
}
}
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的值时将重复出现该菜单。
}
}
你检查一下你这行读到的到底是什么?问题肯定在这里。
你输入的数字\r\n
但原例题是可以的,原题没有if语句,且switch语句在循环外,所以这样接受输入应该可行
//用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;
}
}
}
也就是说,这程序只在输入为0时才正常运行,其它值一概执行“输入错误!”那行。
输入关键字前的数字可查看相应的格式:
1.if
2.switch
3.while
4.do-while
5.for
0.退出 请输入:
1
if的格式为:if(条件)语句体;
else 语句体
输入的值是:1输入的值是:11.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:这格式很头痛
输入关键字前的数字可查看相应的格式:
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楼所说的