//用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的值时将重复出现该菜单。
}
}

解决方案 »

  1.   

    class前加上public 结果和之前一样
      

  2.   

    c = (char) System.in.read(); 
    你检查一下你这行读到的到底是什么?问题肯定在这里。
      

  3.   

    后面加的2行System.out.println("输入的值是:"+c+"\n"); 就是想知道到底读了什么值的,可只有0能显示,其它的都没显示(如注释所说)
      

  4.   

    c = (char) System.in.read();你输入了数字5,转化成char型后就不一定是'5'了!
      

  5.   

    原因找到了。c = (char) System.in.read();这边,System.in里有3个字节:
    你输入的数字\r\n
      

  6.   

    原例题是可以的,原题没有if语句,且switch语句在循环外,所以接受的输入应该是没问题的
      

  7.   

    难道要认为的去掉 多余的字节\r\n?
    但原例题是可以的,原题没有if语句,且switch语句在循环外,所以这样接受输入应该可行
      

  8.   

    第一次输入1-5中的某一个数,按回车(实际是输入了三个字符),read方法仅读取第一个字符,输出正常的结果;进行第二次循环的时候,缓存区内还有字符,故继续读取,此次读取的字符是回车符,所以程序跑到if中,第三次循环,缓存区内仍然有字符,继续读取此次读取的字符是换行符,仍跑到if中,第四次的时候就会提示你再次输入。
      

  9.   

    首先感谢各位学长的解答,但我还是不明白,因为原例题这样读取字符是可行的,下面附上原例题:
    //用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;
        }
     }

      

  10.   

    如果按7楼、12楼的说法,假如我输入3,回车后实际输入的是3和2个其它字符;程序读取了这3个字符中的第1个字符(3),那么程序第一次的运行结果应该是正常的,但实际上却执行了{System.out.println("\n\n输入错误!请输入关键字前面的数字\n"); ,可是输入字符0却能正常输出和运行。
    也就是说,这程序只在输入为0时才正常运行,其它值一概执行“输入错误!”那行。
      

  11.   

    那里有 do  if的结构啊
      

  12.   

    我帮你运行了下,结果如下
    输入关键字前的数字可查看相应的格式:
    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:这格式很头痛
      

  13.   

    原题正确是因为他只做一次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楼所说的